forked from JushuangQiao/Python-Offer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththree.py
More file actions
31 lines (27 loc) · 771 Bytes
/
three.py
File metadata and controls
31 lines (27 loc) · 771 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
26
27
28
29
30
31
# coding=utf-8
# 二维数组中,每行从左到右递增,每列从上到下递增,给出一个数,判断它是否在数组中
# 从左下角(或右上角)开始遍历数组
def find_integer(matrix, num):
"""
:param matrix: [[]]
:param num: int
:return: bool
"""
if not matrix:
return False
rows, cols = len(matrix), len(matrix[0])
row, col = rows - 1, 0
while row >= 0 and col <= cols - 1:
if matrix[row][col] == num:
return True
elif matrix[row][col] > num:
row -= 1
else:
col += 1
return False
if __name__ == '__main__':
matrix = [[1, 2, 3],
[2, 3, 6],
[3, 6, 7]]
num = 6
print find_integer(matrix, num)