Skip to content

Commit fe30290

Browse files
authored
feat: #240 添加python Code
1 parent 045ccbb commit fe30290

File tree

1 file changed

+27
-31
lines changed

1 file changed

+27
-31
lines changed

problems/240.search-a-2-d-matrix-ii.md

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ Given target = 20, return false.
4343

4444
## 代码
4545

46+
代码支持:JavaScript, Python3
47+
48+
49+
JavaScript Code:
50+
4651
```js
4752

4853
/*
@@ -52,37 +57,6 @@ Given target = 20, return false.
5257
*
5358
* https://leetcode.com/problems/search-a-2d-matrix-ii/description/
5459
*
55-
* algorithms
56-
* Medium (40.30%)
57-
* Total Accepted: 170K
58-
* Total Submissions: 419.1K
59-
* Testcase Example: '[[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]]\n5'
60-
*
61-
* Write an efficient algorithm that searches for a value in an m x n matrix.
62-
* This matrix has the following properties:
63-
*
64-
*
65-
* Integers in each row are sorted in ascending from left to right.
66-
* Integers in each column are sorted in ascending from top to bottom.
67-
*
68-
*
69-
* Example:
70-
*
71-
* Consider the following matrix:
72-
*
73-
*
74-
* [
75-
* ⁠ [1, 4, 7, 11, 15],
76-
* ⁠ [2, 5, 8, 12, 19],
77-
* ⁠ [3, 6, 9, 16, 22],
78-
* ⁠ [10, 13, 14, 17, 24],
79-
* ⁠ [18, 21, 23, 26, 30]
80-
* ]
81-
*
82-
*
83-
* Given target = 5, return true.
84-
*
85-
* Given target = 20, return false.
8660
*
8761
*/
8862
/**
@@ -114,3 +88,25 @@ var searchMatrix = function(matrix, target) {
11488
};
11589
```
11690

91+
Python Code:
92+
93+
```python
94+
class Solution:
95+
def searchMatrix(self, matrix, target):
96+
m = len(matrix)
97+
if m == 0:
98+
return False
99+
n = len(matrix[0])
100+
i = m - 1
101+
j = 0
102+
103+
while i >= 0 and j < n:
104+
if matrix[i][j] == target:
105+
return True
106+
if matrix[i][j] > target:
107+
i -= 1
108+
else:
109+
j += 1
110+
return False
111+
```
112+

0 commit comments

Comments
 (0)