附leetcode链接:https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/
1351. Count Negative Numbers in a Sorted Matrix
Given a m*n matrix grid which is sorted in non-increasing order both row-wise and column-wise.
Return the number of negative numbers in grid.
public int countNegatives(int[][] gird) {
int countNeg = 0;
for(int i=0;i<gird.length;i++) {
for(int j=0;j<gird[i].length;j++) {
if(gird[i][j]<0)
countNeg++;
}
}
return countNeg;
}
小结:用两层循环挨个遍历的方法效率低,没有用到已排序的条件,待研究其他方法