-
Notifications
You must be signed in to change notification settings - Fork 0
/
200117-1.cpp
65 lines (62 loc) · 1.13 KB
/
200117-1.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// https://leetcode-cn.com/problems/search-a-2d-matrix/
#include <cstdio>
#include <vector>
using namespace std;
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size();
if (m <= 0) return false;
int n = matrix[0].size();
if (n <= 0) return false;
int i = 0;
int j = m * n - 1;
while (i <= j) {
int k = (i + j) / 2;
int y = k / n;
int x = k % n;
int value = matrix[y][x];
if (value == target) return true;
if (value < target) {
i = k + 1;
} else {
j = k - 1;
}
}
return false;
}
};
void print(const vector<vector<int>>& a)
{
printf("[\n");
for (const auto& r : a) {
printf(" [");
int i = 0;
for (auto e : r) {
printf("%s%d", (i++?",":""), e);
}
printf("]\n");
}
printf("]\n");
}
int main()
{
Solution s;
{
vector<vector<int>> a = {
{1, 3, 5, 7},
{10, 11, 16, 20},
{23, 30, 34, 50}
};
printf("%d\n", s.searchMatrix(a, 3)); // answer: true
}
{
vector<vector<int>> a = {
{1, 3, 5, 7},
{10, 11, 16, 20},
{23, 30, 34, 50}
};
printf("%d\n", s.searchMatrix(a, 13)); // answer: false
}
return 0;
}