def binarySearch(self, nums, l, r, target): if l > r: return False m = l + (r-l)/2 if nums[m] == target: return True elif nums[m] > target: return self.binarySearch(nums, l, m-1, target) else: return self.binarySearch(nums, m+1, r, target)
def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]] :type target: int :rtype: bool """ if matrix==None or len(matrix)==0: return False m = len(matrix) n = len(matrix[0]) i = 0 j = n-1 while i!=m-1 and j!=0: if matrix[i][j] == target: return True elif matrix[i][j] < target: i += 1 else: j -= 1 if i == m-1: return self.binarySearch(matrix[i][:j+1],0,j,target) else: return self.binarySearch([matrix[k][j] for k in range(i,m)],0,m-i-1,target)
评论系统未开启,无法评论!