GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/python/074_Search_a_2D_Matrix.py at master · iamsathish/leetcode Expand file tree/ Copy path074_Search_a_2D_Matrix.pyMore file actions 62 lines (58 loc) · 1.79 KB / Copy path074_Search_a_2D_Matrix.pyFile metadata and controls62 lines (58 loc) · 1.79 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 class Solution(object): # def searchMatrix(self, matrix, target): # """ # :type matrix: List[List[int]] # :type target: int # :rtype: bool # """ # try: # ls_row, ls_col = len(matrix), len(matrix[0]) # except: # return False # begin, end = 0, ls_row - 1 # while begin <= end: # mid = (begin + end) / 2 # res = self.search_row(matrix, mid, target) # if res == 0: # return True # elif res < 0: # end = mid - 1 # else: # begin = mid + 1 # return False # # # def search_row(self, matrix, row, target): # if target < matrix[row][0]: # return -1 # elif target > matrix[row][-1]: # return 1 # begin, end = 0, len(matrix[row]) - 1 # while begin <= end: # mid = (begin + end) / 2 # if matrix[row][mid] == target: # return 0 # elif matrix[row][mid] < target: # begin = mid + 1 # else: # end = mid - 1 # return 1 def searchMatrix(self, matrix, target): # binary search try: ls_row, ls_col = len(matrix), len(matrix[0]) except: return False if target < matrix[0][0] or target > matrix[-1][-1]: return False begin, end = 0, ls_row * ls_col - 1 while begin <= end: mid = (begin + end) / 2 row, col = mid / ls_col, mid % ls_col if matrix[row][col] == target: return True elif matrix[row][col] > target: end = mid - 1 else: begin = mid + 1 return False |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |