fork download
  1. # your code goes here
  2. class Solution(object):
  3. def searchMatrix(self, matrix, target):
  4. """
  5. :type matrix: List[List[int]]
  6. :type target: int
  7. :rtype: bool
  8. """
  9. matrix = [num for row in matrix for num in row]
  10. def condition(mid):
  11. if matrix[mid] == target:
  12. return "found"
  13. elif matrix[mid] > target:
  14. return 'left'
  15. elif matrix[mid] < target:
  16. return 'right'
  17.  
  18.  
  19. def binary_search(lo, hi, condition):
  20. while lo <= hi:
  21. mid = (lo + hi) // 2
  22. result = condition(mid)
  23.  
  24. if result =='found':
  25. return True
  26. elif result == 'left':
  27. hi = mid - 1
  28. elif result == 'right':
  29. lo = mid + 1
  30. return False
  31.  
  32. return binary_search(0, len(matrix)-1, condition)
Success #stdin #stdout 0.13s 14120KB
stdin
Standard input is empty
stdout
Standard output is empty