Skip to content

Commit 42ab5cd

Browse files
authored
Create 74-Search-a-2D-Matrix.py
1 parent b67945a commit 42ab5cd

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

74-Search-a-2D-Matrix.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
3+
ROWS, COLS = len(matrix), len(matrix[0])
4+
5+
top, bot = 0, ROWS - 1
6+
while top <= bot:
7+
row = (top + bot) // 2
8+
if target > matrix[row][-1]:
9+
top = row + 1
10+
elif target < matrix[row][0]:
11+
bot = row - 1
12+
else:
13+
break
14+
15+
if not (top <= bot):
16+
return False
17+
row = (top + bot) // 2
18+
l, r = 0, COLS - 1
19+
while l <= r:
20+
m = (l + r) // 2
21+
if target > matrix[row][m]:
22+
l = m + 1
23+
elif target < matrix[row][m]:
24+
r = m - 1
25+
else:
26+
return True
27+
return False

0 commit comments

Comments
 (0)