leetcode74Search a 2D Matrix

来源:互联网 发布:2016餐饮软件排名 编辑:程序博客网 时间:2024/06/10 09:40
class Solution(object):
    def searchMatrix(self, matrix, target):
        """
        :type matrix: List[List[int]]
        :type target: int
        :rtype: bool
        """
        if not matrix:
            return False
        n=len(matrix)
        m=len(matrix[0])
        l,r=0,n*m-1
        while l<=r:
            mid=l+(r-l)/2
            e=matrix[mid/m][mid%m]
            if e==target:
                return True
            elif e<target:
                l=mid+1
            else:
                r=mid-1
        return False
0 0
原创粉丝点击