-
Notifications
You must be signed in to change notification settings - Fork 0
/
240
39 lines (34 loc) · 979 Bytes
/
240
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
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if(!matrix.size() || !matrix[0].size())
return false;
int m = matrix.size(), n = matrix[0].size();
for(int i = 0; i < m; i ++){
for(int j = 0; j < n; j ++){
if(matrix[i][j] == target)
return true;
else if (matrix[i][j] > target)
n = j;
}
}
return false;
}
};
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if(!matrix.size() || !matrix[0].size())
return false;
int m = matrix.size(), n = matrix[0].size();
int i = m-1, j = 0;
while(i >= 0 && j != n){
if(matrix[i][j] == target)
return true;
else if(matrix[i][j] > target)
i--;
else j++;
}
return false;
}
};