BST分了两个不同的方向,从而可以定位数据。
所以这种“有单调性但不够单调”的查找,就要找到BST的那个出发点/角度,就可以定位数据了。
本题就是右上角或者左下角。例如,从右上角出发,大的在下面,小的在左边。
class Solution:
def Find(self , target: int, array: List[List[int]]) -> bool:
if not array or not array[0]: return False
col, row = len(array), len(array[0])
i, j = 0, row - 1
# 从右上角出发判断,大的在下面,小的在左边
while j >= 0 and i <= col - 1:
if array[i][j] == target: return True
if array[i][j] > target: j -= 1
if array[i][j] < target: i += 1
return False
京公网安备 11010502036488号