49
49
50
50
** 方法一:二分查找**
51
51
52
- 由于每一行升序排列,因此可以对每一行执行二分查找 。
52
+ 由于每一行的所有元素升序排列,因此,对于每一行,我们可以使用二分查找找到第一个大于等于 ` target ` 的元素,然后判断该元素是否等于 ` target ` 。如果等于 ` target ` ,说明找到了目标值,直接返回 ` true ` 。如果不等于 ` target ` ,说明这一行的所有元素都小于 ` target ` ,应该继续搜索下一行 。
53
53
54
- 时间复杂度 $O(mlogn)$。
54
+ 如果所有行都搜索完了,都没有找到目标值,说明目标值不存在,返回 ` false ` 。
55
+
56
+ 时间复杂度 $O(m \times \log n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。
55
57
56
58
** 方法二:从左下角或右上角搜索**
57
59
58
- 这里我们以左下角作为起始搜索点,往右上方向开始搜索,比较当前元素 ` matrix[i][j] ` 与 target 的大小关系:
60
+ 这里我们以左下角作为起始搜索点,往右上方向开始搜索,比较当前元素 ` matrix[i][j] ` 与 ` target ` 的大小关系:
59
61
60
62
- 若 ` matrix[i][j] == target ` ,说明找到了目标值,直接返回 true。
61
63
- 若 ` matrix[i][j] > target ` ,说明这一行从当前位置开始往右的所有元素均大于 target,应该让 i 指针往上移动,即 ` i-- ` 。
62
64
- 若 ` matrix[i][j] < target ` ,说明这一列从当前位置开始往上的所有元素均小于 target,应该让 j 指针往右移动,即 ` j++ ` 。
63
65
64
66
若搜索结束依然找不到 target,返回 false。
65
67
66
- 时间复杂度 $O(m+ n)$。
68
+ 时间复杂度 $O(m + n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数 。
67
69
68
70
<!-- tabs:start -->
69
71
70
72
### ** Python3**
71
73
72
74
<!-- 这里可写当前语言的特殊实现逻辑 -->
73
75
74
- 二分查找:
75
-
76
76
``` python
77
77
class Solution :
78
78
def searchMatrix (self , matrix : List[List[int ]], target : int ) -> bool :
79
- n = len (matrix[0 ])
80
79
for row in matrix:
81
- idx = bisect_left(row, target)
82
- if idx != n and row[idx ] == target:
80
+ j = bisect_left(row, target)
81
+ if j < len (matrix[ 0 ]) and row[j ] == target:
83
82
return True
84
83
return False
85
84
```
86
85
87
- 从左下角或右上角搜索:
88
-
89
86
``` python
90
87
class Solution :
91
88
def searchMatrix (self , matrix : List[List[int ]], target : int ) -> bool :
@@ -105,14 +102,12 @@ class Solution:
105
102
106
103
<!-- 这里可写当前语言的特殊实现逻辑 -->
107
104
108
- 二分查找:
109
-
110
105
``` java
111
106
class Solution {
112
107
public boolean searchMatrix (int [][] matrix , int target ) {
113
- for (int [] row : matrix) {
114
- int idx = Arrays . binarySearch(row, target);
115
- if (idx >= 0 ) {
108
+ for (var row : matrix) {
109
+ int j = Arrays . binarySearch(row, target);
110
+ if (j >= 0 ) {
116
111
return true ;
117
112
}
118
113
}
@@ -121,8 +116,6 @@ class Solution {
121
116
}
122
117
```
123
118
124
- 从左下角或右上角搜索:
125
-
126
119
``` java
127
120
class Solution {
128
121
public boolean searchMatrix (int [][] matrix , int target ) {
@@ -143,85 +136,38 @@ class Solution {
143
136
}
144
137
```
145
138
146
- ### ** TypeScript**
147
-
148
- 二分查找:
149
-
150
- ``` ts
151
- function searchMatrix(matrix : number [][], target : number ): boolean {
152
- const n = matrix [0 ].length ;
153
- for (const row of matrix ) {
154
- let left = 0 ,
155
- right = n ;
156
- while (left < right ) {
157
- const mid = (left + right ) >> 1 ;
158
- if (row [mid ] >= target ) {
159
- right = mid ;
160
- } else {
161
- left = mid + 1 ;
162
- }
163
- }
164
- if (left != n && row [left ] == target ) {
165
- return true ;
166
- }
167
- }
168
- return false ;
169
- }
170
- ```
171
-
172
- 从左下角或右上角搜索:
173
-
174
- ``` ts
175
- function searchMatrix(matrix : number [][], target : number ): boolean {
176
- let m = matrix .length ,
177
- n = matrix [0 ].length ;
178
- let i = m - 1 ,
179
- j = 0 ;
180
- while (i >= 0 && j < n ) {
181
- let cur = matrix [i ][j ];
182
- if (cur == target ) return true ;
183
- if (cur > target ) {
184
- -- i ;
185
- } else {
186
- ++ j ;
187
- }
188
- }
189
- return false ;
190
- }
191
- ```
192
-
193
139
### ** C++**
194
140
195
- 二分查找:
196
-
197
141
``` cpp
198
142
class Solution {
199
143
public:
200
144
bool searchMatrix(vector<vector<int >>& matrix, int target) {
201
- int n = matrix[ 0] .size();
202
145
for (auto& row : matrix) {
203
- int idx = lower_bound(row.begin(), row.end(), target) - row.begin();
204
- if (idx != n && row[ idx] == target) return true;
146
+ int j = lower_bound(row.begin(), row.end(), target) - row.begin();
147
+ if (j < matrix[ 0] .size() && row[ j] == target) {
148
+ return true;
149
+ }
205
150
}
206
151
return false;
207
152
}
208
153
};
209
154
```
210
155
211
- 从左下角或右上角搜索:
212
-
213
156
```cpp
214
157
class Solution {
215
158
public:
216
159
bool searchMatrix(vector<vector<int>>& matrix, int target) {
217
160
int m = matrix.size(), n = matrix[0].size();
218
161
int i = m - 1, j = 0;
219
162
while (i >= 0 && j < n) {
220
- if (matrix[i][j] == target) return true;
221
- if (matrix[i][j] > target)
163
+ if (matrix[i][j] == target) {
164
+ return true;
165
+ }
166
+ if (matrix[i][j] > target) {
222
167
--i;
223
- else
168
+ } else {
224
169
++j;
170
+ }
225
171
}
226
172
return false;
227
173
}
@@ -230,31 +176,18 @@ public:
230
176
231
177
### ** Go**
232
178
233
- 二分查找:
234
-
235
179
``` go
236
180
func searchMatrix (matrix [][]int , target int ) bool {
237
- n := len (matrix[0 ])
238
181
for _ , row := range matrix {
239
- left , right := 0 , n
240
- for left < right {
241
- mid := (left + right) >> 1
242
- if row[mid] >= target {
243
- right = mid
244
- } else {
245
- left = mid + 1
246
- }
247
- }
248
- if left != n && row[left] == target {
182
+ j := sort.SearchInts (row, target)
183
+ if j < len (matrix[0 ]) && row[j] == target {
249
184
return true
250
185
}
251
186
}
252
187
return false
253
188
}
254
189
```
255
190
256
- 从左下角或右上角搜索:
257
-
258
191
``` go
259
192
func searchMatrix (matrix [][]int , target int ) bool {
260
193
m , n := len (matrix), len (matrix[0 ])
@@ -273,25 +206,77 @@ func searchMatrix(matrix [][]int, target int) bool {
273
206
}
274
207
```
275
208
209
+ ### ** TypeScript**
210
+
211
+ ``` ts
212
+ function searchMatrix(matrix : number [][], target : number ): boolean {
213
+ const n = matrix [0 ].length ;
214
+ for (const row of matrix ) {
215
+ let left = 0 ,
216
+ right = n ;
217
+ while (left < right ) {
218
+ const mid = (left + right ) >> 1 ;
219
+ if (row [mid ] >= target ) {
220
+ right = mid ;
221
+ } else {
222
+ left = mid + 1 ;
223
+ }
224
+ }
225
+ if (left != n && row [left ] == target ) {
226
+ return true ;
227
+ }
228
+ }
229
+ return false ;
230
+ }
231
+ ```
232
+
233
+ ``` ts
234
+ function searchMatrix(matrix : number [][], target : number ): boolean {
235
+ let m = matrix .length ,
236
+ n = matrix [0 ].length ;
237
+ let i = m - 1 ,
238
+ j = 0 ;
239
+ while (i >= 0 && j < n ) {
240
+ let cur = matrix [i ][j ];
241
+ if (cur == target ) return true ;
242
+ if (cur > target ) {
243
+ -- i ;
244
+ } else {
245
+ ++ j ;
246
+ }
247
+ }
248
+ return false ;
249
+ }
250
+ ```
251
+
276
252
### ** C#**
277
253
254
+ ``` cs
255
+ public class Solution {
256
+ public bool SearchMatrix (int [][] matrix , int target ) {
257
+ foreach (int [] row in matrix ) {
258
+ int j = Array .BinarySearch (row , target );
259
+ if (j >= 0 ) {
260
+ return true ;
261
+ }
262
+ }
263
+ return false ;
264
+ }
265
+ }
266
+ ```
267
+
278
268
``` cs
279
269
public class Solution {
280
270
public bool SearchMatrix (int [][] matrix , int target ) {
281
271
int m = matrix .Length , n = matrix [0 ].Length ;
282
272
int i = m - 1 , j = 0 ;
283
- while (i >= 0 && j < n )
284
- {
285
- if (matrix [i ][j ] == target )
286
- {
273
+ while (i >= 0 && j < n ) {
274
+ if (matrix [i ][j ] == target ) {
287
275
return true ;
288
276
}
289
- if (matrix [i ][j ] > target )
290
- {
277
+ if (matrix [i ][j ] > target ) {
291
278
-- i ;
292
- }
293
- else
294
- {
279
+ } else {
295
280
++ j ;
296
281
}
297
282
}
0 commit comments