26
26
27
27
## 解法
28
28
29
- ### 方法一
29
+ ### 方法一:二分查找
30
+
31
+ 由于每一行的所有元素升序排列,因此,对于每一行,我们可以使用二分查找找到第一个大于等于 ` target ` 的元素,然后判断该元素是否等于 ` target ` 。如果等于 ` target ` ,说明找到了目标值,直接返回 ` true ` 。如果不等于 ` target ` ,说明这一行的所有元素都小于 ` target ` ,应该继续搜索下一行。
32
+
33
+ 如果所有行都搜索完了,都没有找到目标值,说明目标值不存在,返回 ` false ` 。
34
+
35
+ 时间复杂度 $O(m \times \log n)$,其中 $m$ 和 $n$ 分别为矩阵的行数和列数。空间复杂度 $O(1)$。
36
+
37
+ <!-- tabs:start -->
38
+
39
+ ``` python
40
+ class Solution :
41
+ def searchMatrix (self , matrix : List[List[int ]], target : int ) -> bool :
42
+ for row in matrix:
43
+ j = bisect_left(row, target)
44
+ if j < len (matrix[0 ]) and row[j] == target:
45
+ return True
46
+ return False
47
+ ```
48
+
49
+ ``` java
50
+ class Solution {
51
+ public boolean searchMatrix (int [][] matrix , int target ) {
52
+ for (var row : matrix) {
53
+ int j = Arrays . binarySearch(row, target);
54
+ if (j >= 0 ) {
55
+ return true ;
56
+ }
57
+ }
58
+ return false ;
59
+ }
60
+ }
61
+ ```
62
+
63
+ ``` cpp
64
+ class Solution {
65
+ public:
66
+ bool searchMatrix(vector<vector<int >>& matrix, int target) {
67
+ for (auto& row : matrix) {
68
+ int j = lower_bound(row.begin(), row.end(), target) - row.begin();
69
+ if (j < matrix[ 0] .size() && row[ j] == target) {
70
+ return true;
71
+ }
72
+ }
73
+ return false;
74
+ }
75
+ };
76
+ ```
77
+
78
+ ```go
79
+ func searchMatrix(matrix [][]int, target int) bool {
80
+ for _, row := range matrix {
81
+ j := sort.SearchInts(row, target)
82
+ if j < len(matrix[0]) && row[j] == target {
83
+ return true
84
+ }
85
+ }
86
+ return false
87
+ }
88
+ ```
89
+
90
+ ``` ts
91
+ function searchMatrix(matrix : number [][], target : number ): boolean {
92
+ const n = matrix [0 ].length ;
93
+ for (const row of matrix ) {
94
+ let left = 0 ,
95
+ right = n ;
96
+ while (left < right ) {
97
+ const mid = (left + right ) >> 1 ;
98
+ if (row [mid ] >= target ) {
99
+ right = mid ;
100
+ } else {
101
+ left = mid + 1 ;
102
+ }
103
+ }
104
+ if (left != n && row [left ] == target ) {
105
+ return true ;
106
+ }
107
+ }
108
+ return false ;
109
+ }
110
+ ```
111
+
112
+ ``` rust
113
+ use std :: cmp :: Ordering ;
114
+
115
+ impl Solution {
116
+ pub fn search_matrix (matrix : Vec <Vec <i32 >>, target : i32 ) -> bool {
117
+ let m = matrix . len ();
118
+ let n = matrix [0 ]. len ();
119
+ let mut i = 0 ;
120
+ let mut j = n ;
121
+ while i < m && j > 0 {
122
+ match target . cmp (& matrix [i ][j - 1 ]) {
123
+ Ordering :: Less => {
124
+ j -= 1 ;
125
+ }
126
+ Ordering :: Greater => {
127
+ i += 1 ;
128
+ }
129
+ Ordering :: Equal => {
130
+ return true ;
131
+ }
132
+ }
133
+ }
134
+ false
135
+ }
136
+ }
137
+ ```
138
+
139
+ ``` js
140
+ /**
141
+ * @param {number[][]} matrix
142
+ * @param {number} target
143
+ * @return {boolean}
144
+ */
145
+ var searchMatrix = function (matrix , target ) {
146
+ const n = matrix[0 ].length ;
147
+ for (const row of matrix) {
148
+ let left = 0 ,
149
+ right = n;
150
+ while (left < right) {
151
+ const mid = (left + right) >> 1 ;
152
+ if (row[mid] >= target) {
153
+ right = mid;
154
+ } else {
155
+ left = mid + 1 ;
156
+ }
157
+ }
158
+ if (left != n && row[left] == target) {
159
+ return true ;
160
+ }
161
+ }
162
+ return false ;
163
+ };
164
+ ```
165
+
166
+ ``` cs
167
+ public class Solution {
168
+ public bool SearchMatrix (int [][] matrix , int target ) {
169
+ foreach (int [] row in matrix ) {
170
+ int j = Array .BinarySearch (row , target );
171
+ if (j >= 0 ) {
172
+ return true ;
173
+ }
174
+ }
175
+ return false ;
176
+ }
177
+ }
178
+ ```
179
+
180
+ <!-- tabs: end -->
181
+
182
+ ### 方法二:从左下角或右上角搜索
183
+
184
+ 这里我们以左下角作为起始搜索点,往右上方向开始搜索,比较当前元素 ` matrix[i][j] ` 与 ` target ` 的大小关系:
185
+
186
+ - 若 $\text{matrix}[ i] [ j ] = \text{target}$,说明找到了目标值,直接返回 ` true ` 。
187
+ - 若 $\text{matrix}[ i] [ j ] > \text{target}$,说明这一列从当前位置开始往上的所有元素均大于 ` target ` ,应该让 $i$ 指针往上移动,即 $i \leftarrow i - 1$。
188
+ - 若 $\text{matrix}[ i] [ j ] < \text{target}$,说明这一行从当前位置开始往右的所有元素均小于 ` target ` ,应该让 $j$ 指针往右移动,即 $j \leftarrow j + 1$。
189
+
190
+ 若搜索结束依然找不到 ` target ` ,返回 ` false ` 。
191
+
192
+ 时间复杂度 $O(m + n)$,其中 $m$ 和 $n$ 分别为矩阵的行数和列数。空间复杂度 $O(1)$。
30
193
31
194
<!-- tabs: start -->
32
195
33
196
``` python
34
197
class Solution :
35
198
def searchMatrix (self , matrix : List[List[int ]], target : int ) -> bool :
36
- if not matrix or not matrix[ 0 ] :
199
+ if not matrix:
37
200
return False
38
201
m, n = len (matrix), len (matrix[0 ])
39
202
i, j = m - 1 , 0
@@ -50,7 +213,7 @@ class Solution:
50
213
``` java
51
214
class Solution {
52
215
public boolean searchMatrix (int [][] matrix , int target ) {
53
- if (matrix == null || matrix. length == 0 || matrix[ 0 ] == null || matrix[ 0 ] . length == 0 ) {
216
+ if (matrix == null || matrix. length == 0 ) {
54
217
return false ;
55
218
}
56
219
int m = matrix. length, n = matrix[0 ]. length;
@@ -74,15 +237,20 @@ class Solution {
74
237
class Solution {
75
238
public:
76
239
bool searchMatrix(vector<vector<int >>& matrix, int target) {
77
- if (matrix.size() == 0 || matrix[ 0] .size() == 0) return false;
240
+ if (matrix.empty()) {
241
+ return false;
242
+ }
78
243
int m = matrix.size(), n = matrix[ 0] .size();
79
244
int i = m - 1, j = 0;
80
245
while (i >= 0 && j < n) {
81
- if (matrix[ i] [ j ] == target) return true;
82
- if (matrix[ i] [ j ] > target)
246
+ if (matrix[ i] [ j ] == target) {
247
+ return true;
248
+ }
249
+ if (matrix[ i] [ j ] > target) {
83
250
--i;
84
- else
251
+ } else {
85
252
++j;
253
+ }
86
254
}
87
255
return false;
88
256
}
@@ -91,7 +259,7 @@ public:
91
259
92
260
```go
93
261
func searchMatrix(matrix [][]int, target int) bool {
94
- if len(matrix) == 0 || len(matrix[0]) == 0 {
262
+ if len(matrix) == 0 {
95
263
return false
96
264
}
97
265
m, n := len(matrix), len(matrix[0])
@@ -110,6 +278,50 @@ func searchMatrix(matrix [][]int, target int) bool {
110
278
}
111
279
```
112
280
281
+ ``` ts
282
+ function searchMatrix(matrix : number [][], target : number ): boolean {
283
+ if (matrix .length === 0 ) {
284
+ return false ;
285
+ }
286
+ const [m, n] = [matrix .length , matrix [0 ].length ];
287
+ let [i, j] = [m - 1 , 0 ];
288
+ while (i >= 0 && j < n ) {
289
+ if (matrix [i ][j ] === target ) {
290
+ return true ;
291
+ }
292
+ if (matrix [i ][j ] > target ) {
293
+ -- i ;
294
+ } else {
295
+ ++ j ;
296
+ }
297
+ }
298
+ return false ;
299
+ }
300
+ ```
301
+
302
+ ``` cs
303
+ public class Solution {
304
+ public bool SearchMatrix (int [][] matrix , int target ) {
305
+ if (matrix .Length == 0 ) {
306
+ return false ;
307
+ }
308
+ int m = matrix .Length , n = matrix [0 ].Length ;
309
+ int i = m - 1 , j = 0 ;
310
+ while (i >= 0 && j < n ) {
311
+ if (matrix [i ][j ] == target ) {
312
+ return true ;
313
+ }
314
+ if (matrix [i ][j ] > target ) {
315
+ -- i ;
316
+ } else {
317
+ ++ j ;
318
+ }
319
+ }
320
+ return false ;
321
+ }
322
+ }
323
+ ```
324
+
113
325
<!-- tabs: end -->
114
326
115
327
<!-- end -->
0 commit comments