-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode74.cpp
More file actions
64 lines (38 loc) · 1.01 KB
/
leetcode74.cpp
File metadata and controls
64 lines (38 loc) · 1.01 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Solution {
public:
bool searchInRow(vector<vector<int>>& mat,int midrow,int target){
int n=mat[0].size();
int st=0,end=n-1;
while(st<=end){
int mid=st+(end-st)/2;
if(target==mat[midrow][mid]){
return true;
}
else if(target>mat[midrow][mid]){
st=mid+1;
}
else if(target<mat[midrow][mid]){
end=mid-1;
}
}
return false;
}
bool searchMatrix(vector<vector<int>>& mat, int target) {
int n=mat[0].size(),m=mat.size();
int startrow =0,endrow=m-1;
while(startrow<=endrow){
int midrow=startrow+(endrow-startrow)/2;
if(target>=mat[midrow][0]&&(target<=mat[midrow][n-1])){
return searchInRow(mat, midrow, target);
}
//found row;
else if(target>mat[midrow][n-1]){
startrow=midrow+1;
}
else{
endrow=midrow-1;
}
}
return false;
}
};