-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLeetCode#60.cc
36 lines (33 loc) · 1023 Bytes
/
LeetCode#60.cc
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
class Solution {
public:
bool searchMatrix(vector<vector<int> > &mat, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int n = mat.size();
if(n==0) return false;
int m = mat[0].size();
if(m==0) return false;
int row = -1;
{
int low = 0,high = n-1;
while(low<=high){
int mid = (low+high)/2;
if(mat[mid][0]==target) return true;
else if(target > mat[mid][0]) low = mid+1;
else high = mid-1;
}
if(high==-1) return false;
row = high;
}
{
int low = 0,high = m-1;
while(low<=high){
int mid = (low+high)/2;
if(mat[row][mid]==target) return true;
else if(target > mat[row][mid]) low = mid+1;
else high = mid-1;
}
}
return false;
}
};