LeetCode-74-Search a 2D Matrix 二维二分

来源:互联网 发布:51单片机ad转换 编辑:程序博客网 时间:2024/05/16 23:50
class Solution(object):    def searchMatrix(self, matrix, target):        """        :type matrix: List[List[int]]        :type target: int        :rtype: bool        """        Len=len(matrix)        if Len==0:return False        if Len==1:return self.binarySearch(matrix[0],target)        l=0        r=Len-1        while(l<r):            m=(l+r)/2            if matrix[m][0]<target:                l=m+1            else:                r=m        if matrix[r][0]==target:return True        if r==0:return False        if matrix[r][0]<target:return self.binarySearch(matrix[r],target)        return self.binarySearch(matrix[r-1],target)        def binarySearch(self, s, v):        Len=len(s)        if Len==0:return False        l=0        r=Len-1        while(l<r):            m=(l+r)/2            if s[m]<v:                l=m+1            else:                r=m        if s[r]==v:return True        return False