-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path240.cpp
More file actions
executable file
·24 lines (24 loc) · 818 Bytes
/
240.cpp
File metadata and controls
executable file
·24 lines (24 loc) · 818 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
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if (matrix.size() == 0)
return false;
if (matrix[0].size() == 0)
return false;
for (int i = 0; i < matrix.size(); i++) {
if (target < matrix[i][0] || target > matrix[i][matrix[i].size()-1])
continue;
int st = 0;
int ed = matrix[i].size()-1;
int tmp = 0;
while (st <= ed){
tmp = (st+ed)/2;
if (matrix[i][st] == target || matrix[i][ed] == target || matrix[i][tmp] == target) return true;
if (ed - st <= 1) break;
if (target >= matrix[i][tmp]) st = tmp;
else ed = tmp;
}
}
return false;
}
};