-
Notifications
You must be signed in to change notification settings - Fork 3
/
q0240.py
34 lines (28 loc) · 1016 Bytes
/
q0240.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/python3
from typing import List
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
left = 0
top = 0
down = len(matrix) - 1
if down < 0:
return False
right = len(matrix[0]) - 1
if right < 0:
return False
while left <= right and top <= down:
# print(top, left, down, right)
while top <= down and matrix[top][right] < target:
top += 1
if top <= down and matrix[top][right] == target:
return True
right -= 1
while top <= down and matrix[down][left] > target:
down -= 1
if top <= down and matrix[down][left] == target:
return True
left += 1
return False
matrix = [[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]]
target = 5
print(Solution().findNumberIn2DArray(matrix, target))