-
Notifications
You must be signed in to change notification settings - Fork 16
/
074-Search a 2D Matrix.c
38 lines (38 loc) · 1.26 KB
/
074-Search a 2D Matrix.c
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
bool searchMatrix(int** matrix, int matrixRowSize, int matrixColSize, int target) {
int start = 0, end = matrixRowSize;
int mid = (start + end) / 2;
if (target < matrix[0][0] || target > matrix[matrixRowSize-1][matrixColSize-1]) return false;
while (true) {
if (matrix[mid][0] == target) return true;
if (mid == matrixRowSize - 1 && matrix[mid][0] < target) break;
if (matrix[mid][0] < target && target < matrix[mid+1][0]) break;
if (matrix[mid][0] > target) {
end = mid;
mid = (mid + start) / 2;
} else {
start = mid;
mid = (mid + end) / 2;
}
}
int rowNum = mid;
start = 0;
end = matrixColSize;
mid = (start + end) / 2;
while (true) {
if (matrix[rowNum][mid] == target) return true;
if (mid == matrixColSize - 1 && matrix[rowNum][mid] < target) return false;
if (matrix[rowNum][mid] < target && target < matrix[rowNum][mid+1]) return false;
int premid = mid;
if (matrix[rowNum][mid] > target) {
end = mid;
mid = (mid + start) / 2;
} else {
start = mid;
mid = (mid + end) / 2;
}
if (premid == mid) {
break;
}
}
return false;
}