class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
ans = 0
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] < 0:
ans += (len(grid[i])) - j
break
return ans```
leetcode-1351 统计有序矩阵中的负数
最新推荐文章于 2021-05-23 10:09:49 发布
本文介绍了一种算法,用于统计二维矩阵中负数元素的数量。通过遍历矩阵的每一行,一旦找到第一个负数,即可计算出该行剩余的所有元素均为负数,从而避免了不必要的遍历。
487

被折叠的 条评论
为什么被折叠?



