blob: 08560ef4b2db5a129e12c6ba805183f3e2197886 (
plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#include <vector>
using std::vector;
class Solution
{
public:
bool searchMatrix(vector<vector<int>> &matrix, int target)
{
if (matrix.empty() || matrix.front().empty())
return false;
int top_row = 0;
int bottom_row = matrix.size() - 1;
while (top_row != bottom_row)
{
const int middle_row = (top_row + bottom_row) / 2;
const int middle_row_p1 = middle_row + 1;
const int middle_p1_row_front = matrix[middle_row_p1].front();
if (middle_p1_row_front > target)
{
bottom_row = middle_row;
}
else if (middle_p1_row_front < target)
{
top_row = middle_row_p1;
}
else if (middle_p1_row_front == target)
{
return true;
}
}
const vector<int> &row = matrix[top_row];
if (row.size() == 1)
{
return row.front() == target;
}
int left = 0;
int right = row.size() - 1;
while (left != right)
{
const int middle = (left + right) / 2;
const int middle_value = row[middle];
if (middle_value == target)
{
return true;
}
else if (middle_value < target)
{
left = middle + 1;
}
else
{
right = middle;
}
}
return row[left] == target;
}
};
|