-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc_74.java
More file actions
31 lines (28 loc) · 986 Bytes
/
lc_74.java
File metadata and controls
31 lines (28 loc) · 986 Bytes
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
package leetcode.code;
/**
* 搜索二维矩阵
* 1 2 3 4
* 6 8 9 10
* 23 29 89 99
* 思路:
* 解法一:拉通进行二分查找
* 解法二:两次二分,第一次确定目标值所在的行,第二次确定所在行中的位置
*/
public class lc_74 {
// 解法一
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return false;
int rows = matrix.length, cols = matrix[0].length;
// 二分查找
int l = 0, r = rows * cols - 1;
int pivotIdx, pivotElement;
while (l <= r) { // l与r相等时任然需要进行比较
pivotIdx = (l + r) / 2;
pivotElement = matrix[pivotIdx / cols][pivotIdx % rows];
if (target == pivotElement) return true;
else if (target > pivotElement) l = pivotIdx + 1;
else r = pivotIdx - 1;
}
return false;
}
}