Skip to content

Commit c063b74

Browse files
committed
leetcode
1 parent edcefe8 commit c063b74

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

074._search_a_2d_matrix.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
###74. Search a 2D Matrix
2+
3+
题目:
4+
<https://leetcode.com/problems/search-a-2d-matrix/>
5+
6+
7+
难度:
8+
Easy
9+
10+
11+
12+
13+
二分Search
14+
15+
16+
```
17+
class Solution(object):
18+
def searchMatrix(self, matrix, target):
19+
"""
20+
:type matrix: List[List[int]]
21+
:type target: int
22+
:rtype: bool
23+
"""
24+
if len(matrix) == 0:
25+
return False
26+
else:
27+
n = len(matrix[0])
28+
half = len(matrix)//2
29+
if target >= matrix[half][0] and target <= matrix[half][n-1]:
30+
return self.searchList(matrix[half],target)
31+
elif target < matrix[half][0]:
32+
return self.searchMatrix(matrix[:half],target)
33+
else:
34+
return self.searchMatrix(matrix[half+1:],target)
35+
36+
def searchList(self, lst, target):
37+
if len(lst) == 0:
38+
return False
39+
else:
40+
mid = len(lst) // 2
41+
if target == lst[mid]:
42+
return True
43+
elif target < lst[mid]:
44+
return self.searchList(lst[:mid],target)
45+
else:
46+
return self.searchList(lst[mid+1:],target)
47+
48+
49+
50+
51+
```

0 commit comments

Comments
 (0)