forked from GDSC-IGDTUW-Autumn-of-Code-2022/dsa-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseach_matrix_2d.cpp
51 lines (40 loc) · 1.17 KB
/
seach_matrix_2d.cpp
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
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
//search row first
int low = 0;
int high = matrix.size();
while(low < high) {
int mid = low + (high - low) / 2;
int cur = matrix[mid][0];
if(cur == target) {
return true;
} else if(cur > target) {
high = mid;
} else {
low = mid + 1;
}
}
--low;
if(low < 0) {
return false;
}
return bin(matrix[low], target);
}
bool bin(vector<int>& arr, int target) {
int low = 0;
int high = arr.size();
while(low < high) {
int mid = low + (high - low) / 2;
int cur = arr[mid];
if(cur == target) {
return true;
} else if(cur > target) {
high = mid;
} else {
low = mid + 1;
}
}
return false;
}
};