Skip to content

Commit 1a7ccdb

Browse files
committed
feat: add solutions to lc problem: No.0240
No.0240.Search a 2D Matrix II
1 parent 4d74d95 commit 1a7ccdb

File tree

2 files changed

+162
-174
lines changed

2 files changed

+162
-174
lines changed

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

Lines changed: 84 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -49,43 +49,40 @@
4949

5050
**方法一:二分查找**
5151

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

54-
时间复杂度 $O(mlogn)$。
54+
如果所有行都搜索完了,都没有找到目标值,说明目标值不存在,返回 `false`
55+
56+
时间复杂度 $O(m \times \log n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。
5557

5658
**方法二:从左下角或右上角搜索**
5759

58-
这里我们以左下角作为起始搜索点,往右上方向开始搜索,比较当前元素 `matrix[i][j]`与 target 的大小关系:
60+
这里我们以左下角作为起始搜索点,往右上方向开始搜索,比较当前元素 `matrix[i][j]``target` 的大小关系:
5961

6062
-`matrix[i][j] == target`,说明找到了目标值,直接返回 true。
6163
-`matrix[i][j] > target`,说明这一行从当前位置开始往右的所有元素均大于 target,应该让 i 指针往上移动,即 `i--`
6264
-`matrix[i][j] < target`,说明这一列从当前位置开始往上的所有元素均小于 target,应该让 j 指针往右移动,即 `j++`
6365

6466
若搜索结束依然找不到 target,返回 false。
6567

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

6870
<!-- tabs:start -->
6971

7072
### **Python3**
7173

7274
<!-- 这里可写当前语言的特殊实现逻辑 -->
7375

74-
二分查找:
75-
7676
```python
7777
class Solution:
7878
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
79-
n = len(matrix[0])
8079
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:
8382
return True
8483
return False
8584
```
8685

87-
从左下角或右上角搜索:
88-
8986
```python
9087
class Solution:
9188
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
@@ -105,14 +102,12 @@ class Solution:
105102

106103
<!-- 这里可写当前语言的特殊实现逻辑 -->
107104

108-
二分查找:
109-
110105
```java
111106
class Solution {
112107
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) {
116111
return true;
117112
}
118113
}
@@ -121,8 +116,6 @@ class Solution {
121116
}
122117
```
123118

124-
从左下角或右上角搜索:
125-
126119
```java
127120
class Solution {
128121
public boolean searchMatrix(int[][] matrix, int target) {
@@ -143,85 +136,38 @@ class Solution {
143136
}
144137
```
145138

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-
193139
### **C++**
194140

195-
二分查找:
196-
197141
```cpp
198142
class Solution {
199143
public:
200144
bool searchMatrix(vector<vector<int>>& matrix, int target) {
201-
int n = matrix[0].size();
202145
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+
}
205150
}
206151
return false;
207152
}
208153
};
209154
```
210155
211-
从左下角或右上角搜索:
212-
213156
```cpp
214157
class Solution {
215158
public:
216159
bool searchMatrix(vector<vector<int>>& matrix, int target) {
217160
int m = matrix.size(), n = matrix[0].size();
218161
int i = m - 1, j = 0;
219162
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) {
222167
--i;
223-
else
168+
} else {
224169
++j;
170+
}
225171
}
226172
return false;
227173
}
@@ -230,31 +176,18 @@ public:
230176

231177
### **Go**
232178

233-
二分查找:
234-
235179
```go
236180
func searchMatrix(matrix [][]int, target int) bool {
237-
n := len(matrix[0])
238181
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 {
249184
return true
250185
}
251186
}
252187
return false
253188
}
254189
```
255190

256-
从左下角或右上角搜索:
257-
258191
```go
259192
func searchMatrix(matrix [][]int, target int) bool {
260193
m, n := len(matrix), len(matrix[0])
@@ -273,25 +206,77 @@ func searchMatrix(matrix [][]int, target int) bool {
273206
}
274207
```
275208

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+
276252
### **C#**
277253

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+
278268
```cs
279269
public class Solution {
280270
public bool SearchMatrix(int[][] matrix, int target) {
281271
int m = matrix.Length, n = matrix[0].Length;
282272
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) {
287275
return true;
288276
}
289-
if (matrix[i][j] > target)
290-
{
277+
if (matrix[i][j] > target) {
291278
--i;
292-
}
293-
else
294-
{
279+
} else {
295280
++j;
296281
}
297282
}

0 commit comments

Comments
 (0)