-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0074.java
More file actions
41 lines (37 loc) · 1.08 KB
/
Copy pathLeetCode0074.java
File metadata and controls
41 lines (37 loc) · 1.08 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
/* Search a 2D Matrix
* Example1:
* Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
* target = 3
* Output: true
* */
public class LeetCode0074 {
public static void main(String args[]) {
int[][] matrix = {{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 50}};
System.out.println(searchMatrix(matrix, 3));
}
public static boolean searchMatrix(int[][] matrix, int target) {
if (matrix.length == 0)
return false;
int row = matrix.length;
int column = matrix[0].length;
int left = 0;
int right = row * column - 1;
while (left <= right) {
int middle = (left + right) / 2;
int middleNum = matrix[middle / column][middle % column];
System.out.println(middleNum);
if (middleNum == target)
return true;
else if (middleNum > target)
right = middle - 1;
else
left = middle + 1;
}
return false;
}
}