题目
暴力方法:
class Solution {
public int countNegatives(int[][] grid) {
int count = 0;
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[0].length;j++){
if(grid[i][j]<0) count++;
}
}
return count;
}
}
从左下角开始走动,方向为向右和向上。
class Solution {
public int countNegatives(int[][] grid) {
int row = grid.length;
int col = grid[0].length;
int count = 0;
int i = row-1,j=0;
while(i>=0 && j<col){
if(grid[i][j]>=0){
j++;
}else{
count += col-j;
i--;
}
}
return count;
}
}