美文网首页
1351. Count Negative Numbers in

1351. Count Negative Numbers in

作者: 30岁每天进步一点点 | 来源:发表于2020-03-07 21:42 被阅读0次

附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;
}

小结:用两层循环挨个遍历的方法效率低,没有用到已排序的条件,待研究其他方法

相关文章

网友评论

      本文标题:1351. Count Negative Numbers in

      本文链接:https://www.haomeiwen.com/subject/lwhkdhtx.html