题解 | #二维数组中的查找#
二维数组中的查找
https://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param target int整型 # @param array int整型二维数组 # @return bool布尔型 # class Solution: # 二分查找 def binsearch(self, data, k): left, right = 0, len(data) - 1 # 第一种写法 left <= right 判定等于 while left <= right: mid = (left + right) // 2 # 重新划分边界 从mid开始 if data[mid] < k: left = mid + 1 elif data[mid] > k: right = mid - 1 else: return True def Find(self , target: int, array: List[List[int]]) -> bool: # write code here # m+n 常数级别 if not array: return False row_length = len(array) col_length = len(array[0]) i = row_length - 1 j = 0 # 开始找 避免出界 # 左下角 比它大的在右边 比它小的在上面 while i >= 0 and j < col_length: if array[i][j] < target: j += 1 elif array[i][j] > target: i -= 1 else: return True return False # 每一层二分查找 nlogn # for i in array: # if self.binsearch(i,target) == True: # return True # else: # continue # return False