Accepted O(logm + logn) in Python


  • 0
    X
    class Solution(object):
        def searchMatrix(self, matrix, target):
            """
            :type matrix: List[List[int]]
            :type target: int
            :rtype: bool
            """
            if not matrix:return False
            m = len(matrix); n = len(matrix[0])
            left = 0; right = m*n - 1
            while left + 1 < right:
                mid = (left+right)/2
                row = mid / n; col = mid % n
                if matrix[row][col] == target:return True
                elif matrix[row][col] < target:
                    left = mid
                else:
                    right = mid
            if matrix[left/n][left%n] == target:return True
            elif matrix[right/n][right%n] == target:return True
            return False

Log in to reply
 

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