-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL1351.java
More file actions
53 lines (47 loc) · 1.35 KB
/
L1351.java
File metadata and controls
53 lines (47 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.liang.leetcode.binarySearch;
/**
* @ClassName: L1351
* @Description: 统计有序矩阵中的负数
* @Author: LiaNg
* @Date: 2020/3/19 11:24
*/
public class L1351 {
public static void main(String[] args) {
L1351 l1351 = new L1351();
int[][] grid = new int[][]{{4, 3, 2, -1}, {3, 2, 1, -1}, {1, 1, -1, -2}, {-1, -1, -2, -3}};
System.out.println("l1351.countNegatives(grid) = " + l1351.countNegatives(grid));
}
/**
* 给你一个 m * n 的矩阵 grid,矩阵中的元素无论是按行还是按列,都以非递增顺序排列。
* 请你统计并返回 grid 中 负数 的数目。
* 提示:
* m == grid.length
* n == grid[i].length
* 1 <= m, n <= 100
* -100 <= grid[i][j] <= 100
*/
public int countNegatives(int[][] grid) {
if (grid == null || grid.length == 0) {
return 0;
}
int result = 0;
for (int[] ints : grid) {
result += ints.length - binarySearch(ints);
}
return result;
}
private int binarySearch(int[] row) {
int l = 0;
int r = row.length - 1;
int m;
while (l <= r) {
m = (l + r) / 2;
if (row[m] >= 0) {
l = m + 1;
} else {
r = m - 1;
}
}
return l;
}
}