Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fd093f8

Browse files
committed
feat: add solutions to lc problem: No.0074
No.0074.Search a 2D Matrix
1 parent 39a782c commit fd093f8

File tree

3 files changed

+236
-20
lines changed

3 files changed

+236
-20
lines changed

‎solution/0000-0099/0074.Search a 2D Matrix/README.md‎

Lines changed: 134 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,32 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
**方法一:二分查找**
48+
4749
将二维矩阵逻辑展开,然后二分查找即可。
4850

51+
时间复杂度 O(logmn)。
52+
53+
**方法二:从左下角或右上角搜索**
54+
55+
这里我们以左下角作为起始搜索点,往右上方向开始搜索,比较当前元素 `matrix[i][j]`与 target 的大小关系:
56+
57+
-`matrix[i][j] == target`,说明找到了目标值,直接返回 true。
58+
-`matrix[i][j] > target`,说明这一行从当前位置开始往右的所有元素均大于 target,应该让 i 指针往上移动,即 `i--`
59+
-`matrix[i][j] < target`,说明这一列从当前位置开始往上的所有元素均小于 target,应该让 j 指针往右移动,即 `j++`
60+
61+
若搜索结束依然找不到 target,返回 false。
62+
63+
时间复杂度 O(m + n)。
64+
4965
<!-- tabs:start -->
5066

5167
### **Python3**
5268

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

71+
二分查找:
72+
5573
```python
5674
class Solution:
5775
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
@@ -67,10 +85,29 @@ class Solution:
6785
return matrix[left // n][left % n] == target
6886
```
6987

88+
从左下角或右上角搜索:
89+
90+
```python
91+
class Solution:
92+
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
93+
m, n = len(matrix), len(matrix[0])
94+
i, j = m - 1, 0
95+
while i >= 0 and j < n:
96+
if matrix[i][j] == target:
97+
return True
98+
if matrix[i][j] > target:
99+
i -= 1
100+
else:
101+
j += 1
102+
return False
103+
```
104+
70105
### **Java**
71106

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

109+
二分查找:
110+
74111
```java
75112
class Solution {
76113
public boolean searchMatrix(int[][] matrix, int target) {
@@ -90,8 +127,31 @@ class Solution {
90127
}
91128
```
92129

130+
从左下角或右上角搜索:
131+
132+
```java
133+
class Solution {
134+
public boolean searchMatrix(int[][] matrix, int target) {
135+
int m = matrix.length, n = matrix[0].length;
136+
for (int i = m - 1, j = 0; i >= 0 && j < n;) {
137+
if (matrix[i][j] == target) {
138+
return true;
139+
}
140+
if (matrix[i][j] > target) {
141+
--i;
142+
} else {
143+
++j;
144+
}
145+
}
146+
return false;
147+
}
148+
}
149+
```
150+
93151
### **C++**
94152

153+
二分查找:
154+
95155
```cpp
96156
class Solution {
97157
public:
@@ -112,35 +172,82 @@ public:
112172
};
113173
```
114174
175+
从左下角或右上角搜索:
176+
177+
```cpp
178+
class Solution {
179+
public:
180+
bool searchMatrix(vector<vector<int>>& matrix, int target) {
181+
int m = matrix.size(), n = matrix[0].size();
182+
for (int i = m - 1, j = 0; i >= 0 && j < n;)
183+
{
184+
if (matrix[i][j] == target) return true;
185+
if (matrix[i][j] > target) --i;
186+
else ++j;
187+
}
188+
return false;
189+
}
190+
};
191+
```
192+
115193
### **JavaScript**
116194

195+
二分查找:
196+
117197
```js
118198
/**
119199
* @param {number[][]} matrix
120200
* @param {number} target
121201
* @return {boolean}
122202
*/
123203
var searchMatrix = function (matrix, target) {
124-
const m = matrix.length;
125-
const n = matrix[0].length;
126-
let left = 0;
127-
let right = m * n - 1;
204+
const m = matrix.length,
205+
n = matrix[0].length;
206+
let left = 0,
207+
right = m * n - 1;
128208
while (left < right) {
129-
const mid = (left + right) >> 1;
209+
const mid = (left + right+1) >> 1;
130210
const x = Math.floor(mid / n);
131211
const y = mid % n;
132-
if (matrix[x][y] >= target) {
133-
right = mid;
212+
if (matrix[x][y] <= target) {
213+
left = mid;
134214
} else {
135-
left = mid + 1;
215+
right = mid - 1;
136216
}
137217
}
138218
return matrix[Math.floor(left / n)][left % n] == target;
139219
};
140220
```
141221

222+
从左下角或右上角搜索:
223+
224+
```js
225+
/**
226+
* @param {number[][]} matrix
227+
* @param {number} target
228+
* @return {boolean}
229+
*/
230+
var searchMatrix = function (matrix, target) {
231+
const m = matrix.length,
232+
n = matrix[0].length;
233+
for (let i = m - 1, j = 0; i >= 0 && j < n; ) {
234+
if (matrix[i][j] == target) {
235+
return true;
236+
}
237+
if (matrix[i][j] > target) {
238+
--i;
239+
} else {
240+
++j;
241+
}
242+
}
243+
return false;
244+
};
245+
```
246+
142247
### **Go**
143248

249+
二分查找:
250+
144251
```go
145252
func searchMatrix(matrix [][]int, target int) bool {
146253
m, n := len(matrix), len(matrix[0])
@@ -158,6 +265,25 @@ func searchMatrix(matrix [][]int, target int) bool {
158265
}
159266
```
160267

268+
从左下角或右上角搜索:
269+
270+
```go
271+
func searchMatrix(matrix [][]int, target int) bool {
272+
m, n := len(matrix), len(matrix[0])
273+
for i, j := m-1, 0; i >= 0 && j < n; {
274+
if matrix[i][j] == target {
275+
return true
276+
}
277+
if matrix[i][j] > target {
278+
i--
279+
} else {
280+
j++
281+
}
282+
}
283+
return false
284+
}
285+
```
286+
161287
### **...**
162288

163289
```

‎solution/0000-0099/0074.Search a 2D Matrix/README_EN.md‎

Lines changed: 98 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ class Solution:
5757
return matrix[left // n][left % n] == target
5858
```
5959

60+
```python
61+
class Solution:
62+
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
63+
m, n = len(matrix), len(matrix[0])
64+
i, j = m - 1, 0
65+
while i >= 0 and j < n:
66+
if matrix[i][j] == target:
67+
return True
68+
if matrix[i][j] > target:
69+
i -= 1
70+
else:
71+
j += 1
72+
return False
73+
```
74+
6075
### **Java**
6176

6277
```java
@@ -78,6 +93,25 @@ class Solution {
7893
}
7994
```
8095

96+
```java
97+
class Solution {
98+
public boolean searchMatrix(int[][] matrix, int target) {
99+
int m = matrix.length, n = matrix[0].length;
100+
for (int i = m - 1, j = 0; i >= 0 && j < n;) {
101+
if (matrix[i][j] == target) {
102+
return true;
103+
}
104+
if (matrix[i][j] > target) {
105+
--i;
106+
} else {
107+
++j;
108+
}
109+
}
110+
return false;
111+
}
112+
}
113+
```
114+
81115
### **C++**
82116

83117
```cpp
@@ -100,6 +134,22 @@ public:
100134
};
101135
```
102136
137+
```cpp
138+
class Solution {
139+
public:
140+
bool searchMatrix(vector<vector<int>>& matrix, int target) {
141+
int m = matrix.size(), n = matrix[0].size();
142+
for (int i = m - 1, j = 0; i >= 0 && j < n;)
143+
{
144+
if (matrix[i][j] == target) return true;
145+
if (matrix[i][j] > target) --i;
146+
else ++j;
147+
}
148+
return false;
149+
}
150+
};
151+
```
152+
103153
### **JavaScript**
104154

105155
```js
@@ -109,24 +159,47 @@ public:
109159
* @return {boolean}
110160
*/
111161
var searchMatrix = function (matrix, target) {
112-
const m = matrix.length;
113-
const n = matrix[0].length;
114-
let left = 0;
115-
let right = m * n - 1;
162+
const m = matrix.length,
163+
n = matrix[0].length;
164+
let left = 0,
165+
right = m * n - 1;
116166
while (left < right) {
117-
const mid = (left + right) >> 1;
167+
const mid = (left + right+1) >> 1;
118168
const x = Math.floor(mid / n);
119169
const y = mid % n;
120-
if (matrix[x][y] >= target) {
121-
right = mid;
170+
if (matrix[x][y] <= target) {
171+
left = mid;
122172
} else {
123-
left = mid + 1;
173+
right = mid - 1;
124174
}
125175
}
126176
return matrix[Math.floor(left / n)][left % n] == target;
127177
};
128178
```
129179

180+
```js
181+
/**
182+
* @param {number[][]} matrix
183+
* @param {number} target
184+
* @return {boolean}
185+
*/
186+
var searchMatrix = function (matrix, target) {
187+
const m = matrix.length,
188+
n = matrix[0].length;
189+
for (let i = m - 1, j = 0; i >= 0 && j < n; ) {
190+
if (matrix[i][j] == target) {
191+
return true;
192+
}
193+
if (matrix[i][j] > target) {
194+
--i;
195+
} else {
196+
++j;
197+
}
198+
}
199+
return false;
200+
};
201+
```
202+
130203
### **Go**
131204

132205
```go
@@ -146,6 +219,23 @@ func searchMatrix(matrix [][]int, target int) bool {
146219
}
147220
```
148221

222+
```go
223+
func searchMatrix(matrix [][]int, target int) bool {
224+
m, n := len(matrix), len(matrix[0])
225+
for i, j := m-1, 0; i >= 0 && j < n; {
226+
if matrix[i][j] == target {
227+
return true
228+
}
229+
if matrix[i][j] > target {
230+
i--
231+
} else {
232+
j++
233+
}
234+
}
235+
return false
236+
}
237+
```
238+
149239
### **...**
150240

151241
```

‎solution/0000-0099/0074.Search a 2D Matrix/Solution.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
* @return {boolean}
55
*/
66
var searchMatrix = function (matrix, target) {
7-
const m = matrix.length;
8-
const n = matrix[0].length;
9-
let left = 0;
10-
let right = m * n - 1;
7+
const m = matrix.length,
8+
n = matrix[0].length;
9+
let left = 0,
10+
right = m * n - 1;
1111
while (left < right) {
1212
const mid = (left + right + 1) >> 1;
1313
const x = Math.floor(mid / n);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /