Python Binary Search


  • 0
    Y
    def searchMatrix(self, matrix, target):
            low = 0
            m = len(matrix)
            n = len(matrix[0])
            high = m * n - 1
            mid = (low + high) / 2
        
            while (low < high - 1):
                mid = (low + high) / 2
                if target < matrix[mid / n][mid % n]:
                    high = mid
                elif target > matrix[mid / n][mid % n]:
                    low = mid
                else:
                    return True
            return target == matrix[low / n][low % n] or target == matrix[high / n][high % n]

Log in to reply
 

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