-
Notifications
You must be signed in to change notification settings - Fork 5
/
04.py
28 lines (25 loc) · 814 Bytes
/
04.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
from typing import List
class Solution:
def findNumberIn2DArray(self, matrix: List[List[int]], target: int) -> bool:
# solution one: 暴力
# 特判
if matrix == []:
return False
n, m = len(matrix), len(matrix[0])
for i in range(n):
for j in range(m):
if matrix[i][j] == target:
return True
elif matrix[i][j] > target:
break
return False
# solution two: 左下角标志数法
i, j = len(matrix) - 1, 0
while i >= 0 and j < len(matrix[0]):
if matrix[i][j] == target:
return True
elif matrix[i][j] > target:
i -= 1
else:
j += 1
return False