Template
finish 01-homework
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
'''
|
||||
Author: SJ2050
|
||||
Date: 2021-10-16 22:04:07
|
||||
LastEditTime: 2021-10-16 23:25:30
|
||||
Version: v0.0.1
|
||||
Description: Solution for homework7.
|
||||
Copyright © 2021 SJ2050
|
||||
'''
|
||||
def searchMatrix(matrix, target):
|
||||
if (len(matrix) == 0 ):
|
||||
return False
|
||||
|
||||
i = len(matrix)-1
|
||||
j = 0
|
||||
while (i>=0 and j < len(matrix[0])):
|
||||
if (matrix[i][j] == target):
|
||||
return True
|
||||
elif (matrix[i][j] < target):
|
||||
j += 1
|
||||
elif(matrix[i][j] > target):
|
||||
i -= 1
|
||||
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
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 = 20
|
||||
print(searchMatrix(matrix, target))
|
||||
|
||||
# 算法说明:
|
||||
# 从矩阵左下角,先排除不符合条件的列,再排除不符合条件的行,将范围一直往右上角缩小
|
||||
Reference in New Issue
Block a user