AC, in place one binary search with complexity O(lg(m*n))


  • 0
    H
    class Solution:
        # @param {integer[][]} matrix
        # @param {integer} target
        # @return {boolean}
        def searchMatrix(self, matrix, target):
            left,right=0,len(matrix)*len(matrix[0])-1
            while left<=right:
                mid=(left+right)/2
                tmp=matrix[mid/len(matrix[0])][mid%len(matrix[0])]
                if target<tmp:
                    right=mid-1
                elif target>tmp:
                    left=mid+1
                else:
                    return True
            return False

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.