Skip to content

Commit fe9e273

Browse files
committed
feat: add python and java solutions to lcci question
添加《程序员面试金典》题解:面试题 10.09. 排序矩阵查找
1 parent dca9082 commit fe9e273

File tree

7 files changed

+189
-71
lines changed

7 files changed

+189
-71
lines changed

lcci/10.09.Sorted Matrix Search/README.md

+35-2
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,53 @@
2424

2525
## 解法
2626
<!-- 这里可写通用的实现逻辑 -->
27-
27+
从左下角(或右上角)开始查找即可。
2828

2929
### Python3
3030
<!-- 这里可写当前语言的特殊实现逻辑 -->
3131

3232
```python
33+
class Solution:
34+
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
35+
if not matrix or not matrix[0]:
36+
return False
37+
rows, cols = len(matrix), len(matrix[0])
38+
i, j = rows - 1, 0
39+
while i >= 0 and j < cols:
40+
if matrix[i][j] == target:
41+
return True
42+
if matrix[i][j] > target:
43+
i -= 1
44+
else:
45+
j += 1
46+
return False
3347

3448
```
3549

3650
### Java
3751
<!-- 这里可写当前语言的特殊实现逻辑 -->
3852

3953
```java
40-
54+
class Solution {
55+
public boolean searchMatrix(int[][] matrix, int target) {
56+
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
57+
return false;
58+
}
59+
int rows = matrix.length, cols = matrix[0].length;
60+
int i = rows - 1, j = 0;
61+
while (i >= 0 && j < cols) {
62+
if (matrix[i][j] == target) {
63+
return true;
64+
}
65+
if (matrix[i][j] > target) {
66+
--i;
67+
} else {
68+
++j;
69+
}
70+
}
71+
return false;
72+
}
73+
}
4174
```
4275

4376
### ...
+96-44
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,96 @@
1-
# [10.09. Sorted Matrix Search](https://leetcode-cn.com/problems/sorted-matrix-search-lcci)
2-
3-
## Description
4-
<p>Given an M x N matrix in which each row and each column is sorted in ascending order, write a method to find an element.</p>
5-
6-
<p><strong>Example:</strong></p>
7-
8-
<p>Given matrix:</p>
9-
10-
<pre>
11-
[
12-
[1, 4, 7, 11, 15],
13-
[2, 5, 8, 12, 19],
14-
[3, 6, 9, 16, 22],
15-
[10, 13, 14, 17, 24],
16-
[18, 21, 23, 26, 30]
17-
]
18-
</pre>
19-
20-
<p>Given target&nbsp;=&nbsp;5,&nbsp;return&nbsp;<code>true.</code></p>
21-
22-
<p>Given target&nbsp;=&nbsp;20, return&nbsp;<code>false.</code></p>
23-
24-
25-
26-
## Solutions
27-
28-
29-
### Python3
30-
31-
```python
32-
33-
```
34-
35-
### Java
36-
37-
```java
38-
39-
```
40-
41-
### ...
42-
```
43-
44-
```
1+
# [10.09. Sorted Matrix Search](https://leetcode-cn.com/problems/sorted-matrix-search-lcci)
2+
3+
## Description
4+
<p>Given an M x N matrix in which each row and each column is sorted in ascending order, write a method to find an element.</p>
5+
6+
7+
8+
<p><strong>Example:</strong></p>
9+
10+
11+
12+
<p>Given matrix:</p>
13+
14+
15+
16+
<pre>
17+
18+
[
19+
20+
[1, 4, 7, 11, 15],
21+
22+
[2, 5, 8, 12, 19],
23+
24+
[3, 6, 9, 16, 22],
25+
26+
[10, 13, 14, 17, 24],
27+
28+
[18, 21, 23, 26, 30]
29+
30+
]
31+
32+
</pre>
33+
34+
35+
36+
<p>Given target&nbsp;=&nbsp;5,&nbsp;return&nbsp;<code>true.</code></p>
37+
38+
39+
40+
<p>Given target&nbsp;=&nbsp;20, return&nbsp;<code>false.</code></p>
41+
42+
43+
44+
45+
## Solutions
46+
47+
48+
### Python3
49+
50+
```python
51+
class Solution:
52+
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
53+
if not matrix or not matrix[0]:
54+
return False
55+
rows, cols = len(matrix), len(matrix[0])
56+
i, j = rows - 1, 0
57+
while i >= 0 and j < cols:
58+
if matrix[i][j] == target:
59+
return True
60+
if matrix[i][j] > target:
61+
i -= 1
62+
else:
63+
j += 1
64+
return False
65+
66+
```
67+
68+
### Java
69+
70+
```java
71+
class Solution {
72+
public boolean searchMatrix(int[][] matrix, int target) {
73+
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
74+
return false;
75+
}
76+
int rows = matrix.length, cols = matrix[0].length;
77+
int i = rows - 1, j = 0;
78+
while (i >= 0 && j < cols) {
79+
if (matrix[i][j] == target) {
80+
return true;
81+
}
82+
if (matrix[i][j] > target) {
83+
--i;
84+
} else {
85+
++j;
86+
}
87+
}
88+
return false;
89+
}
90+
}
91+
```
92+
93+
### ...
94+
```
95+
96+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public boolean searchMatrix(int[][] matrix, int target) {
3+
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
4+
return false;
5+
}
6+
int rows = matrix.length, cols = matrix[0].length;
7+
int i = rows - 1, j = 0;
8+
while (i >= 0 && j < cols) {
9+
if (matrix[i][j] == target) {
10+
return true;
11+
}
12+
if (matrix[i][j] > target) {
13+
--i;
14+
} else {
15+
++j;
16+
}
17+
}
18+
return false;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
3+
if not matrix or not matrix[0]:
4+
return False
5+
rows, cols = len(matrix), len(matrix[0])
6+
i, j = rows - 1, 0
7+
while i >= 0 and j < cols:
8+
if matrix[i][j] == target:
9+
return True
10+
if matrix[i][j] > target:
11+
i -= 1
12+
else:
13+
j += 1
14+
return False

lcof/面试题04. 二维数组中的查找/README.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,38 @@
3434
```python
3535
class Solution:
3636
def findNumberIn2DArray(self, matrix: List[List[int]], target: int) -> bool:
37-
if not matrix:
37+
if not matrix or not matrix[0]:
3838
return False
39-
40-
i, j = len(matrix) - 1, 0
41-
while i >= 0 and j < len(matrix[0]):
39+
rows, cols = len(matrix), len(matrix[0])
40+
i, j = rows - 1, 0
41+
while i >= 0 and j < cols:
4242
if matrix[i][j] == target:
4343
return True
44-
if matrix[i][j] < target:
45-
j += 1
46-
else:
44+
if matrix[i][j] > target:
4745
i -= 1
46+
else:
47+
j += 1
4848
return False
49+
4950
```
5051

5152
### Java
5253
```java
5354
class Solution {
5455
public boolean findNumberIn2DArray(int[][] matrix, int target) {
55-
if (matrix == null || matrix.length == 0) {
56+
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
5657
return false;
5758
}
5859
int rows = matrix.length, cols = matrix[0].length;
59-
6060
int i = rows - 1, j = 0;
6161
while (i >= 0 && j < cols) {
6262
if (matrix[i][j] == target) {
6363
return true;
6464
}
65-
if (matrix[i][j] < target) {
66-
++j;
67-
} else {
65+
if (matrix[i][j] > target) {
6866
--i;
67+
} else {
68+
++j;
6969
}
7070
}
7171
return false;

lcof/面试题04. 二维数组中的查找/Solution.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
class Solution {
22
public boolean findNumberIn2DArray(int[][] matrix, int target) {
3-
if (matrix == null || matrix.length == 0) {
3+
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
44
return false;
55
}
66
int rows = matrix.length, cols = matrix[0].length;
7-
87
int i = rows - 1, j = 0;
98
while (i >= 0 && j < cols) {
109
if (matrix[i][j] == target) {
1110
return true;
1211
}
13-
if (matrix[i][j] < target) {
14-
++j;
15-
} else {
12+
if (matrix[i][j] > target) {
1613
--i;
14+
} else {
15+
++j;
1716
}
1817
}
1918
return false;
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class Solution:
22
def findNumberIn2DArray(self, matrix: List[List[int]], target: int) -> bool:
3-
if not matrix:
3+
if not matrix or not matrix[0]:
44
return False
5-
6-
i, j = len(matrix) - 1, 0
7-
while i >= 0 and j < len(matrix[0]):
5+
rows, cols = len(matrix), len(matrix[0])
6+
i, j = rows - 1, 0
7+
while i >= 0 and j < cols:
88
if matrix[i][j] == target:
99
return True
10-
if matrix[i][j] < target:
11-
j += 1
12-
else:
10+
if matrix[i][j] > target:
1311
i -= 1
14-
return False
12+
else:
13+
j += 1
14+
return False

0 commit comments

Comments
 (0)