Skip to content

Commit d526be5

Browse files
committed
1 parent f85b032 commit d526be5

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

l33tcode/search-a-2d-matrix.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1+
from typing import List
2+
13
class Solution:
24
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
35
if not matrix:
46
return False
57

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]
711

8-
while left <= right:
9-
mid = (left + right) // 2
12+
left, right = 0, len(matrix) * len(matrix[0]) - 1
1013

11-
row = mid // len(matrix[0])
12-
col = mid % len(matrix[0])
14+
while left < right:
15+
mid = left + (right - left) // 2
1316

14-
if matrix[row][col] < target:
17+
if get_value(mid) < target:
1518
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
2021

21-
return False
22+
return get_value(left) == target

0 commit comments

Comments
 (0)