-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main4.java
53 lines (50 loc) · 1.2 KB
/
Main4.java
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 JZOffer2;
/**
* 二分查找
*/
public class Main4 {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
for (int[] ints : matrix) {
if (findSearch(ints, target)) {
return true;
}
}
return false;
}
private boolean findSearch(int[] nums, int target) {
int n = nums.length;
int left = 0, right = n - 1;
while (left <= right) {
int mid = (right - left) / 2 + left;
if(nums[mid] == target) {
return true;
}
else if(nums[mid] < target) {
left = mid - 1;
} else {
right = mid + 1;
}
}
return false;
}
}
/**
* Z 字形查找
*/
class Main4_1 {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
int m = matrix.length, n = matrix[0].length;
int x = 0, y = n - 1;
while (x < m && y >= 0){
if(matrix[x][y] == target) {
return true;
}
if(matrix[x][y] > target) {
--y;
} else {
++x;
}
}
return false;
}
}