print('Hello, world!')
l = [1,2,3,4,5,6,7,8,9,10]
target = 4
def binarySearch(l):
left = 0
right = len(l) - 1
step = 0
while left <= right:
pivot = (left + right) // 2
if l[pivot] > target:
right = pivot - 1
elif l[pivot] < target:
left = pivot + 1
else :
return pivot
return -1
print(binarySearch(l))
재귀 / 반복문으로 풀 수 있지만, 이왕이면 재귀를 지양하자는 의미에서 반복문으로만 풀어봄
'Algorithm > 기본 알고리즘' 카테고리의 다른 글
| boj-11866. 요세푸스 문제 (0) | 2023.01.19 |
|---|---|
| leetcode 21. Merge Two Sorted Lists (0) | 2022.08.28 |
| 지도에서 상하좌우 움직이기 (0) | 2022.08.27 |
| leetcode 206. Reverse Linked List (0) | 2022.04.05 |
| leetcode 46. Permutations 순열 (0) | 2022.04.03 |