这次在leetcode上选取的是array里面的74. Search a 2D Matrix(原题链接),题目如下:
源码如下:
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if matrix == [[]]:
return False
if matrix == []:
return False
else:
i, j = 0, len(matrix[0]) - 1
while i < len(matrix) and j >= 0:
if matrix[i][j] == target:
return True
elif matrix[i][j] < target:
i += 1
else:
j -= 1
return False
通过后的截图: