-
Notifications
You must be signed in to change notification settings - Fork 1
/
search2Dmatrix.cpp
43 lines (39 loc) · 987 Bytes
/
search2Dmatrix.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
#include<iostream>
#include<vector>
using namespace std;
class Play {
public:
bool searchMatrix(vector<vector<int> > &mat, int t) {
int m = mat.size(), n = mat[0].size();
int l = 0, h = m - 1;
while(l <= h) {
int mid = (l+h) / 2;
if(mat[mid][0] == t)
return true;
else if(mat[mid][0] > t)
h = mid - 1;
else
l = mid + 1;
}
if(h < 0) return false;
m = h, l = 0, h = n - 1;
while(l <= h) {
int mid = (l+h) / 2;
if(mat[m][mid] == t)
return true;
else if(mat[m][mid] > t)
h = mid - 1;
else
l = mid + 1;
}
return false;
}
};
int main() {
vector<vector<int> > mat {{1, 4, 6, 8},
{9, 13, 15, 20},{30, 40, 50, 60}};
Play pp;
if(pp.searchMatrix(mat, 50))
cout << "Found !\n";
return 0;
}