Skip to content

Commit ae59d0d

Browse files
authoredApr 30, 2024··
feat: add solutions to lcci problem: No.10.09 (#2699)
1 parent 9e8c8a5 commit ae59d0d

19 files changed

+684
-87
lines changed
 

‎lcci/10.09.Sorted Matrix Search/README.md

+220-8
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,177 @@
2626

2727
## 解法
2828

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)$。
30193

31194
<!-- tabs:start -->
32195

33196
```python
34197
class Solution:
35198
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
36-
if not matrix or not matrix[0]:
199+
if not matrix:
37200
return False
38201
m, n = len(matrix), len(matrix[0])
39202
i, j = m - 1, 0
@@ -50,7 +213,7 @@ class Solution:
50213
```java
51214
class Solution {
52215
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) {
54217
return false;
55218
}
56219
int m = matrix.length, n = matrix[0].length;
@@ -74,15 +237,20 @@ class Solution {
74237
class Solution {
75238
public:
76239
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+
}
78243
int m = matrix.size(), n = matrix[0].size();
79244
int i = m - 1, j = 0;
80245
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) {
83250
--i;
84-
else
251+
} else {
85252
++j;
253+
}
86254
}
87255
return false;
88256
}
@@ -91,7 +259,7 @@ public:
91259
92260
```go
93261
func searchMatrix(matrix [][]int, target int) bool {
94-
if len(matrix) == 0 || len(matrix[0]) == 0 {
262+
if len(matrix) == 0 {
95263
return false
96264
}
97265
m, n := len(matrix), len(matrix[0])
@@ -110,6 +278,50 @@ func searchMatrix(matrix [][]int, target int) bool {
110278
}
111279
```
112280

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+
113325
<!-- tabs:end -->
114326

115327
<!-- end -->

0 commit comments

Comments
 (0)
Please sign in to comment.