forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
33 lines (31 loc) · 872 Bytes
/
solution.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
class Solution
{
public:
int transform(vector<vector<int> > &matrix, int index)
{
return matrix[index/matrix[0].size()][index%matrix[0].size()];
}
int bSearch(vector<vector<int> > &matrix, int begin, int end, int target)
{
if(begin <= end)
{
int mid = (begin+end)/2;
int temp = transform(matrix,mid);
if(target == temp)
return mid;
else if(target < temp)
return bSearch(matrix,begin,mid-1,target);
else
return bSearch(matrix,mid+1,end,target);
}
else
return -1;
}
bool searchMatrix(vector<vector<int> > &matrix, int target)
{
if (bSearch(matrix,0,matrix.size()*matrix[0].size()-1,target) >= 0)
return true;
else
return false;
}
};