1351. Count Negative Numbers in

2020-03-07  本文已影响0人  30岁每天进步一点点

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

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

上一篇 下一篇

猜你喜欢

热点阅读