File tree 1 file changed +12
-11
lines changed
1 file changed +12
-11
lines changed Original file line number Diff line number Diff line change
1
+ from typing import List
2
+
1
3
class Solution :
2
4
def searchMatrix (self , matrix : List [List [int ]], target : int ) -> bool :
3
5
if not matrix :
4
6
return False
5
7
6
- left , right = 0 , len (matrix ) * len (matrix [0 ]) - 1
8
+ def get_value (pos : int ):
9
+ row , col = pos // len (matrix [0 ]), pos % len (matrix [0 ])
10
+ return matrix [row ][col ]
7
11
8
- while left <= right :
9
- mid = (left + right ) // 2
12
+ left , right = 0 , len (matrix ) * len (matrix [0 ]) - 1
10
13
11
- row = mid // len ( matrix [ 0 ])
12
- col = mid % len ( matrix [ 0 ])
14
+ while left < right :
15
+ mid = left + ( right - left ) // 2
13
16
14
- if matrix [ row ][ col ] < target :
17
+ if get_value ( mid ) < target :
15
18
left = mid + 1
16
- elif matrix [row ][col ] > target :
17
- right = mid - 1
18
- else :
19
- return True
19
+ elif get_value (mid ) >= target :
20
+ right = mid
20
21
21
- return False
22
+ return get_value ( left ) == target
You can’t perform that action at this time.
0 commit comments