-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode240.cpp
74 lines (69 loc) · 1.95 KB
/
leetcode240.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
66
67
68
69
70
71
72
73
74
/*************************************************
Author: wenhaofang
Date: 2023-04-15
Description: leetcode240 - Search a 2D Matrix II
*************************************************/
#include <bits/stdc++.h>
using namespace std;
/**
* 方法一:二分查找
*
* 理论时间复杂度:
* 理论空间复杂度:O(1)
*/
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size();
int n = matrix[0].size();
return recursive(matrix, target, 0, 0, m - 1, n - 1);
}
bool recursive(vector<vector<int>>& matrix, int target, int x1, int y1, int x2, int y2) {
int m = matrix.size();
int n = matrix[0].size();
if (
x1 < 0 || x1 >= m ||
x2 < 0 || x2 >= m ||
y1 < 0 || y1 >= n ||
y2 < 0 || y2 >= n ||
x1 > x2 ||
y1 > y2
) {
return false;
}
int xm = (x1 + x2) / 2;
int ym = (y1 + y2) / 2;
if (matrix[xm][ym] == target) {
return true;
}
else if (matrix[xm][ym] > target) {
return (
recursive(matrix, target, xm, y1, x2, ym - 1) ||
recursive(matrix, target, x1, y1, xm - 1, y2)
);
}
else if (matrix[xm][ym] < target) {
return (
recursive(matrix, target, x1, ym + 1, xm, y2) ||
recursive(matrix, target, xm + 1, y1, x2, y2)
);
}
return false;
}
};
/**
* 测试
*/
int main() {
Solution* solution = new Solution();
vector<vector<int>> matrix = {
{1 , 4 , 7 , 11, 15},
{2 , 5 , 8 , 12, 19},
{3 , 6 , 9 , 16, 22},
{10, 13, 14, 17, 24},
{18, 21, 23, 26, 30}
};
int target = 5;
bool ans = solution -> searchMatrix(matrix, target);
cout << ans << endl;
}