-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path74.search-a-2d-matrix.cpp
More file actions
35 lines (31 loc) · 903 Bytes
/
74.search-a-2d-matrix.cpp
File metadata and controls
35 lines (31 loc) · 903 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
32
33
34
35
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size();
int n = matrix[0].size();
int targetrow = -1;
for(int i = 0; i<m; i++){
if(matrix[i][n-1] >= target){
targetrow = i;
break;
}
}
if(targetrow == -1)
return false;
int start = 0, end = n-1;
while(start<=end){
int mid = (start+end)/2;
int val = matrix[targetrow][mid];
if(val==target){
return true;
}
else if(val>target)
{
end = mid-1;
}else{
start = mid+1;
}
}
return false;
}
};