Skip to content

Commit 73793ca

Browse files
authored
feat: add solutions to lc problem: No.240 (#3898)
No.0240.Search a 2D Matrix II
1 parent 988caec commit 73793ca

File tree

7 files changed

+109
-87
lines changed

7 files changed

+109
-87
lines changed

solution/0000-0099/0048.Rotate Image/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ tags:
5858

5959
### 方法一:原地翻转
6060

61-
根据题目要求,我们实际上需要将 $matrix[i][j]$ 旋转至 $matrix[j][n - i - 1]$。
61+
根据题目要求,我们实际上需要将 $\text{matrix}[i][j]$ 旋转至 $\text{matrix}[j][n - i - 1]$。
6262

63-
我们可以先对矩阵进行上下翻转,即 $matrix[i][j]$ 和 $matrix[n - i - 1][j]$ 进行交换,然后再对矩阵进行主对角线翻转,即 $matrix[i][j]$ 和 $matrix[j][i]$ 进行交换。这样就能将 $matrix[i][j]$ 旋转至 $matrix[j][n - i - 1]$ 了。
63+
我们可以先对矩阵进行上下翻转,即 $\text{matrix}[i][j]$ 和 $\text{matrix}[n - i - 1][j]$ 进行交换,然后再对矩阵进行主对角线翻转,即 $\text{matrix}[i][j]$ 和 $\text{matrix}[j][i]$ 进行交换。这样就能将 $\text{matrix}[i][j]$ 旋转至 $\text{matrix}[j][n - i - 1]$ 了。
6464

6565
时间复杂度 $O(n^2)$,其中 $n$ 是矩阵的边长。空间复杂度 $O(1)$。
6666

solution/0000-0099/0048.Rotate Image/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ tags:
5454

5555
### Solution 1: In-place Rotation
5656

57-
According to the problem requirements, we actually need to rotate $matrix[i][j]$ to $matrix[j][n - i - 1]$.
57+
According to the problem requirements, we need to rotate $\text{matrix}[i][j]$ to $\text{matrix}[j][n - i - 1]$.
5858

59-
We can first flip the matrix upside down, that is, swap $matrix[i][j]$ and $matrix[n - i - 1][j]$, and then flip the matrix along the main diagonal, that is, swap $matrix[i][j]$ and $matrix[j][i]$. This way, we can rotate $matrix[i][j]$ to $matrix[j][n - i - 1]$.
59+
We can first flip the matrix upside down, i.e., swap $\text{matrix}[i][j]$ with $\text{matrix}[n - i - 1][j]$, and then flip the matrix along the main diagonal, i.e., swap $\text{matrix}[i][j]$ with $\text{matrix}[j][i]$. This way, we can rotate $\text{matrix}[i][j]$ to $\text{matrix}[j][n - i - 1]$.
6060

6161
The time complexity is $O(n^2)$, where $n$ is the side length of the matrix. The space complexity is $O(1)$.
6262

solution/0200-0299/0240.Search a 2D Matrix II/README.md

+38-29
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ tags:
6464

6565
### 方法一:二分查找
6666

67-
由于每一行的所有元素升序排列,因此,对于每一行,我们可以使用二分查找找到第一个大于等于 `target` 的元素,然后判断该元素是否等于 `target`。如果等于 `target`,说明找到了目标值,直接返回 `true`。如果不等于 `target`,说明这一行的所有元素都小于 `target`,应该继续搜索下一行。
67+
由于每一行的所有元素升序排列,因此,对于每一行,我们可以使用二分查找找到第一个大于等于 $\textit{target}$ 的元素,然后判断该元素是否等于 $\textit{target}$。如果等于 $\textit{target}$,说明找到了目标值,直接返回 $\text{true}$。如果不等于 $\textit{target}$,说明这一行的所有元素都小于 $\textit{target}$,应该继续搜索下一行。
6868

69-
如果所有行都搜索完了,都没有找到目标值,说明目标值不存在,返回 `false`
69+
如果所有行都搜索完了,都没有找到目标值,说明目标值不存在,返回 $\text{false}$
7070

7171
时间复杂度 $O(m \times \log n)$,其中 $m$ 和 $n$ 分别为矩阵的行数和列数。空间复杂度 $O(1)$。
7272

@@ -137,17 +137,8 @@ func searchMatrix(matrix [][]int, target int) bool {
137137
function searchMatrix(matrix: number[][], target: number): boolean {
138138
const n = matrix[0].length;
139139
for (const row of matrix) {
140-
let left = 0,
141-
right = n;
142-
while (left < right) {
143-
const mid = (left + right) >> 1;
144-
if (row[mid] >= target) {
145-
right = mid;
146-
} else {
147-
left = mid + 1;
148-
}
149-
}
150-
if (left != n && row[left] == target) {
140+
const j = _.sortedIndex(row, target);
141+
if (j < n && row[j] === target) {
151142
return true;
152143
}
153144
}
@@ -195,17 +186,8 @@ impl Solution {
195186
var searchMatrix = function (matrix, target) {
196187
const n = matrix[0].length;
197188
for (const row of matrix) {
198-
let left = 0,
199-
right = n;
200-
while (left < right) {
201-
const mid = (left + right) >> 1;
202-
if (row[mid] >= target) {
203-
right = mid;
204-
} else {
205-
left = mid + 1;
206-
}
207-
}
208-
if (left != n && row[left] == target) {
189+
const j = _.sortedIndex(row, target);
190+
if (j < n && row[j] == target) {
209191
return true;
210192
}
211193
}
@@ -237,13 +219,13 @@ public class Solution {
237219

238220
### 方法二:从左下角或右上角搜索
239221

240-
这里我们以左下角作为起始搜索点,往右上方向开始搜索,比较当前元素 `matrix[i][j]``target` 的大小关系:
222+
这里我们以左下角或右上角作为起始搜索点,往右上或左下方向开始搜索。比较当前元素 $\textit{matrix}[i][j]$ 与 $\textit{target}$ 的大小关系:
241223

242-
- 若 $\textit{matrix}[i][j] = \textit{target}$,说明找到了目标值,直接返回 `true`
243-
- 若 $\textit{matrix}[i][j] > \textit{target}$,说明这一列从当前位置开始往上的所有元素均大于 `target`,应该让 $i$ 指针往上移动,即 $i \leftarrow i - 1$。
244-
- 若 $\textit{matrix}[i][j] < \textit{target}$,说明这一行从当前位置开始往右的所有元素均小于 `target`,应该让 $j$ 指针往右移动,即 $j \leftarrow j + 1$。
224+
- 若 $\textit{matrix}[i][j] = \textit{target}$,说明找到了目标值,直接返回 $\text{true}$
225+
- 若 $\textit{matrix}[i][j] > \textit{target}$,说明这一列从当前位置开始往上的所有元素均大于 $\textit{target}$,应该让 $i$ 指针往上移动,即 $i \leftarrow i - 1$。
226+
- 若 $\textit{matrix}[i][j] < \textit{target}$,说明这一行从当前位置开始往右的所有元素均小于 $\textit{target}$,应该让 $j$ 指针往右移动,即 $j \leftarrow j + 1$。
245227

246-
若搜索结束依然找不到 `target`,返回 `false`
228+
若搜索结束依然找不到 $\textit{target}$,返回 $\text{false}$
247229

248230
时间复杂度 $O(m + n)$,其中 $m$ 和 $n$ 分别为矩阵的行数和列数。空间复杂度 $O(1)$。
249231

@@ -351,6 +333,33 @@ function searchMatrix(matrix: number[][], target: number): boolean {
351333
}
352334
```
353335

336+
#### Rust
337+
338+
```rust
339+
impl Solution {
340+
pub fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool {
341+
let m = matrix.len();
342+
let n = matrix[0].len();
343+
let mut i = m - 1;
344+
let mut j = 0;
345+
while i >= 0 && j < n {
346+
if matrix[i][j] == target {
347+
return true;
348+
}
349+
if matrix[i][j] > target {
350+
if i == 0 {
351+
break;
352+
}
353+
i -= 1;
354+
} else {
355+
j += 1;
356+
}
357+
}
358+
false
359+
}
360+
}
361+
```
362+
354363
#### C#
355364

356365
```cs

solution/0200-0299/0240.Search a 2D Matrix II/README_EN.md

+41-32
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ tags:
6262

6363
### Solution 1: Binary Search
6464

65-
Since all elements in each row are sorted in ascending order, we can use binary search to find the first element that is greater than or equal to `target` for each row, and then check if this element is equal to `target`. If it equals `target`, it means the target value has been found, and we directly return `true`. If it does not equal `target`, it means all elements in this row are less than `target`, and we should continue to search the next row.
65+
Since all elements in each row are sorted in ascending order, for each row, we can use binary search to find the first element greater than or equal to $\textit{target}$, and then check if that element is equal to $\textit{target}$. If it is equal to $\textit{target}$, it means the target value is found, and we return $\text{true}$. If it is not equal to $\textit{target}$, it means all elements in this row are less than $\textit{target}$, and we should continue searching the next row.
6666

67-
If all rows have been searched and the target value has not been found, it means the target value does not exist, so we return `false`.
67+
If all rows have been searched and the target value is not found, it means the target value does not exist, and we return $\text{false}$.
6868

69-
The time complexity is $O(m \times \log n)$, where $m$ and $n$ are the number of rows and columns in the matrix, respectively. The space complexity is $O(1)$.
69+
The time complexity is $O(m \times \log n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. The space complexity is $O(1)$.
7070

7171
<!-- tabs:start -->
7272

@@ -135,17 +135,8 @@ func searchMatrix(matrix [][]int, target int) bool {
135135
function searchMatrix(matrix: number[][], target: number): boolean {
136136
const n = matrix[0].length;
137137
for (const row of matrix) {
138-
let left = 0,
139-
right = n;
140-
while (left < right) {
141-
const mid = (left + right) >> 1;
142-
if (row[mid] >= target) {
143-
right = mid;
144-
} else {
145-
left = mid + 1;
146-
}
147-
}
148-
if (left != n && row[left] == target) {
138+
const j = _.sortedIndex(row, target);
139+
if (j < n && row[j] === target) {
149140
return true;
150141
}
151142
}
@@ -193,17 +184,8 @@ impl Solution {
193184
var searchMatrix = function (matrix, target) {
194185
const n = matrix[0].length;
195186
for (const row of matrix) {
196-
let left = 0,
197-
right = n;
198-
while (left < right) {
199-
const mid = (left + right) >> 1;
200-
if (row[mid] >= target) {
201-
right = mid;
202-
} else {
203-
left = mid + 1;
204-
}
205-
}
206-
if (left != n && row[left] == target) {
187+
const j = _.sortedIndex(row, target);
188+
if (j < n && row[j] == target) {
207189
return true;
208190
}
209191
}
@@ -233,17 +215,17 @@ public class Solution {
233215

234216
<!-- solution:start -->
235217

236-
### Solution 2: Search from the Bottom Left or Top Right
218+
### Solution 2: Search from Bottom-Left or Top-Right
237219

238-
Here, we start searching from the bottom left corner and move towards the top right direction, comparing the current element `matrix[i][j]` with `target`:
220+
We start the search from the bottom-left or top-right corner and move towards the top-right or bottom-left direction. Compare the current element $\textit{matrix}[i][j]$ with $\textit{target}$:
239221

240-
- If $\textit{matrix}[i][j] = \textit{target}$, it means the target value has been found, and we directly return `true`.
241-
- If $\textit{matrix}[i][j] > \textit{target}$, it means all elements in this column from the current position upwards are greater than `target`, so we should move the $i$ pointer upwards, i.e., $i \leftarrow i - 1$.
242-
- If $\textit{matrix}[i][j] < \textit{target}$, it means all elements in this row from the current position to the right are less than `target`, so we should move the $j$ pointer to the right, i.e., $j \leftarrow j + 1$.
222+
- If $\textit{matrix}[i][j] = \textit{target}$, it means the target value is found, and we return $\text{true}$.
223+
- If $\textit{matrix}[i][j] > \textit{target}$, it means all elements in this column from the current position upwards are greater than $\textit{target}$, so we move the $i$ pointer upwards, i.e., $i \leftarrow i - 1$.
224+
- If $\textit{matrix}[i][j] < \textit{target}$, it means all elements in this row from the current position to the right are less than $\textit{target}$, so we move the $j$ pointer to the right, i.e., $j \leftarrow j + 1$.
243225

244-
If the search ends and the `target` is still not found, return `false`.
226+
If the search ends and the $\textit{target}$ is not found, return $\text{false}$.
245227

246-
The time complexity is $O(m + n)$, where $m$ and $n$ are the number of rows and columns in the matrix, respectively. The space complexity is $O(1)$.
228+
The time complexity is $O(m + n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. The space complexity is $O(1)$.
247229

248230
<!-- tabs:start -->
249231

@@ -349,6 +331,33 @@ function searchMatrix(matrix: number[][], target: number): boolean {
349331
}
350332
```
351333

334+
#### Rust
335+
336+
```rust
337+
impl Solution {
338+
pub fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool {
339+
let m = matrix.len();
340+
let n = matrix[0].len();
341+
let mut i = m - 1;
342+
let mut j = 0;
343+
while i >= 0 && j < n {
344+
if matrix[i][j] == target {
345+
return true;
346+
}
347+
if matrix[i][j] > target {
348+
if i == 0 {
349+
break;
350+
}
351+
i -= 1;
352+
} else {
353+
j += 1;
354+
}
355+
}
356+
false
357+
}
358+
}
359+
```
360+
352361
#### C#
353362

354363
```cs

solution/0200-0299/0240.Search a 2D Matrix II/Solution.js

+2-11
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,8 @@
66
var searchMatrix = function (matrix, target) {
77
const n = matrix[0].length;
88
for (const row of matrix) {
9-
let left = 0,
10-
right = n;
11-
while (left < right) {
12-
const mid = (left + right) >> 1;
13-
if (row[mid] >= target) {
14-
right = mid;
15-
} else {
16-
left = mid + 1;
17-
}
18-
}
19-
if (left != n && row[left] == target) {
9+
const j = _.sortedIndex(row, target);
10+
if (j < n && row[j] == target) {
2011
return true;
2112
}
2213
}

solution/0200-0299/0240.Search a 2D Matrix II/Solution.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
function searchMatrix(matrix: number[][], target: number): boolean {
22
const n = matrix[0].length;
33
for (const row of matrix) {
4-
let left = 0,
5-
right = n;
6-
while (left < right) {
7-
const mid = (left + right) >> 1;
8-
if (row[mid] >= target) {
9-
right = mid;
10-
} else {
11-
left = mid + 1;
12-
}
13-
}
14-
if (left != n && row[left] == target) {
4+
const j = _.sortedIndex(row, target);
5+
if (j < n && row[j] === target) {
156
return true;
167
}
178
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
pub fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool {
3+
let m = matrix.len();
4+
let n = matrix[0].len();
5+
let mut i = m - 1;
6+
let mut j = 0;
7+
while i >= 0 && j < n {
8+
if matrix[i][j] == target {
9+
return true;
10+
}
11+
if matrix[i][j] > target {
12+
if i == 0 {
13+
break;
14+
}
15+
i -= 1;
16+
} else {
17+
j += 1;
18+
}
19+
}
20+
false
21+
}
22+
}

0 commit comments

Comments
 (0)