-
Notifications
You must be signed in to change notification settings - Fork 464
Expand file tree
/
Copy path74_Search_a_2D_Matrix.py
More file actions
25 lines (22 loc) · 639 Bytes
/
74_Search_a_2D_Matrix.py
File metadata and controls
25 lines (22 loc) · 639 Bytes
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
# Approach 4: Search Space Reduction
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if len(matrix) == 0 or len(matrix[0]) == 0:
return False
height = len(matrix)
weidth = len(matrix[0])
row = height - 1
col = 0
while row >= 0 and col < weidth:
if matrix[row][col] < target:
col += 1
elif matrix[row][col] > target:
row -= 1
else:
return True
return False