forked from geemaple/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path74.search-a-2d-matrix.cpp
44 lines (37 loc) · 1.01 KB
/
74.search-a-2d-matrix.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
44
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if (matrix.size() == 0)
{
return false;
}
int row = (int)matrix.size();
int column = (int)matrix[0].size();
if (column == 0)
{
return false;
}
int start = 0;
int end = row * column - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (matrix[mid / column][mid % column] > target)
{
end = mid;
}
else if (matrix[mid / column][mid % column] < target)
{
start = mid;
}
else
{
return true;
}
}
if (matrix[start / column][start % column] == target || matrix[end / column][end % column] == target)
{
return true;
}
return false;
}
};