-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchA2dMatrix.java
More file actions
200 lines (182 loc) · 5.47 KB
/
Copy pathSearchA2dMatrix.java
File metadata and controls
200 lines (182 loc) · 5.47 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Source : https://leetcode.com/problems/search-a-2d-matrix/
// Author : cornprincess
// Date : 2021-03-30
/*****************************************************************************************************
*
* Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the
* following properties:
*
* Integers in each row are sorted from left to right.
* The first integer of each row is greater than the last integer of the previous row.
*
* Example 1:
*
* Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
* Output: true
*
* Example 2:
*
* Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
* Output: false
*
* Constraints:
*
* m == matrix.length
* n == matrix[i].length
* 1 <= m, n <= 100
* -104 <= matrix[i][j], target <= 104
******************************************************************************************************/
package SearchA2dMatrix;
public class SearchA2dMatrix {
// brute force
// 时间复杂度:O(N * M)
// 空间复杂度:O(1)
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null) {
return false;
}
for (int[] ints : matrix) {
for (int j = 0; j < matrix[0].length; j++) {
if (ints[j] == target) {
return true;
}
}
}
return false;
}
// trick
// 时间复杂度:O(N + M)
// 空间复杂度:O(1)
public boolean searchMatrix2(int[][] matrix, int target) {
int rows = matrix.length;
int cols = matrix[0].length;
int row = 0;
int col = cols - 1;
if (target < matrix[0][0] || target > matrix[rows - 1][cols - 1]) {
return false;
}
while (row < rows && col >= 0) {
if (matrix[row][col] < target) {
row++;
} else if (matrix[row][col] > target) {
col--;
} else {
return true;
}
}
return false;
}
// binary search
// 时间复杂度:O(logN + logM)
// 空间复杂度:O(1)
public boolean searchMatrix3(int[][] matrix, int target) {
int rows = matrix.length;
int cols = matrix[0].length;
if (target < matrix[0][0] || target > matrix[rows - 1][cols - 1]) {
return false;
}
// 第一次二分,找到所在行
// 1 3 4 17 18
// left right mid value target
// 0 4 2 4 9
// 3 4 3 7 9
// 4 4 4 8 9
// 5 4 break
// left right mid value target
// 0 4 2 4 6
// 3 4 3 17 6
// 3 2 break
int left = 0;
int right = rows - 1;
int mid = 0;
while (left <= right) {
mid = left + ((right - left) >> 1);
if (matrix[mid][0] < target) {
left = mid + 1;
} else if (matrix[mid][0] > target) {
right = mid - 1;
} else {
return true;
}
}
if (matrix[mid][0] > target) {
mid--;
}
// 第二次二分,找到所在列
left = 0;
right = cols - 1;
int row = mid;
while (left <= right) {
mid = left + ((right - left) >> 1);
if (matrix[row][mid] > target) {
right = mid - 1;
} else if (matrix[row][mid] < target) {
left = mid + 1;
} else {
return true;
}
}
return false;
}
// binary search
// 时间复杂度:O(logN*M)
// 空间复杂度:O(1)
// 1 3 5 7
// 10 11 16 20
// 23 30 34 60
public boolean searchMatrix4(int[][] matrix, int target) {
int rows = matrix.length;
int cols = matrix[0].length;
if (target < matrix[0][0] || target > matrix[rows - 1][cols - 1]) {
return false;
}
int left = 1;
int right = rows * cols;
int mid;
int row;
int col;
while (left <= right) {
mid = left + ((right - left) >> 1);
if (mid % cols == 0) {
row = mid / cols - 1;
col = cols - 1;
} else {
row = mid / cols;
col = mid % cols - 1;
}
if (matrix[row][col] > target) {
right = mid - 1;
} else if (matrix[row][col] < target) {
left = mid + 1;
} else {
return true;
}
}
return false;
}
public boolean searchMatrix5(int[][] matrix, int target) {
int rows = matrix.length;
int cols = matrix[0].length;
if (target < matrix[0][0] || target > matrix[rows - 1][cols - 1]) {
return false;
}
int left = 0;
int right = rows * cols - 1;
int mid;
int row;
int col;
while (left <= right) {
mid = left + ((right - left) >> 1);
row = mid / cols;
col = mid % cols;
if (matrix[row][col] > target) {
right = mid - 1;
} else if (matrix[row][col] < target) {
left = mid + 1;
} else {
return true;
}
}
return false;
}
}