Re: [閒聊] 每日LeetCode已回收
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/description/
1351. Count Negative Numbers in a Sorted Matrix
給你一個排序好的 Matrix,找出該 Matrix 有幾個非正整數。
Example 1:
Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
Output: 8
Explanation: There are 8 negatives number in the matrix.
Example 2:
Input: grid = [[3,2],[1,0]]
Output: 0
思路:
1.從每一列的最右邊往左找,如果小於0 count就遞增,否則跳出處理下一列。
Java Code:
------------------------------------------------------------
class Solution {
public int countNegatives(int[][] grid) {
int count = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = grid[0].length - 1; j >= 0; j--) {
if (grid[i][j] >= 0) {
break;
}
count++;
}
}
return count;
}
}
------------------------------------------------------------
--
https://i.imgur.com/3e5CZfj.jpg

--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.100.75.86 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1686192740.A.C4A.html
推
06/08 11:22,
2年前
, 1F
06/08 11:22, 1F
推
06/08 11:47,
2年前
, 2F
06/08 11:47, 2F
推
06/08 12:04,
2年前
, 3F
06/08 12:04, 3F
→
06/08 12:30,
2年前
, 4F
06/08 12:30, 4F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 340 之 719 篇):