바이너리 서치 잘 외웠다고 생각했는데,
중간값 계산을 while문 앞에서 해서 고생했습니다. ㅎㅎ
리스트가 비었는지 확인하는 방법은
if not matrix:
가 있는데, 이것 보다
if len(matrix) == 0:
으로 하는게 더 빠르네요 ㅎㅎ
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
def BinarySearch2(target, start, end, matrix, m):
while start <= end:
n = (start + end) // 2
if matrix[m][n] == target:
return True
elif matrix[m][n] > target:
end = n-1
elif matrix[m][n] < target:
start = n + 1
return False
def BinarySearch(target, start, end, matrix):
while start <= end:
m = (start + end) // 2
print(start, end, m, matrix[m][0], target)
if matrix[m][0] == target:
return True
elif matrix[m][0] < target:
start = m + 1
elif matrix[m][0] > target:
end = m - 1
print(end)
print(start, end)
return BinarySearch2(target, 0, len(matrix[0])-1, matrix, end)
if len(matrix) == 0:
return False
if len(matrix[0]) == 0:
return False
return BinarySearch(target, 0, len(matrix)-1, matrix)
'Algorithm > LeetCode 문제 풀이' 카테고리의 다른 글
[LeetCode 70. Climbing Stairs] Easy (0) | 2019.06.10 |
---|---|
[LeetCode 35. Search Insert Position] Easy 3/5 26/150 99.12% (0) | 2019.05.16 |
[LeetCode 69. Sqrt(x)] Easy, Binary Search 1/5 24/150 (0) | 2019.05.15 |
[LeetCode 378. Kth Smallest Element in a Sorted Matrix ] Stack Easy (19주차 9/10, 23/150) (0) | 2019.05.12 |
[LeetCode 155. Min Stack ] Stack Easy (19주차 8/10, 22/150) (0) | 2019.05.12 |