大致思路:二维数组有序排列,右比左大,下比上大。故以左下(右上也可以)为起点,target比当前数字大就右移,小就上移。
时间复杂度O(m + n),空间复杂度O(1),其中m、n为二维数组的行、列
#time:2023-08-09 07:48
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param target int整型
# @param array int整型二维数组
# @return bool布尔型
#
class Solution:
def Find(self , target: int, array: List[List[int]]) -> bool:
# write code here
i, j = len(array) - 1, 0
while i >= 0 and j < len(array[0]):
if target > array[i][j]:
j += 1
elif target < array[i][j]:
i -= 1
else:
return True
return False