이진 탐색 연습하기
이진 탐색 연습하기 — #LeetCode #개발자의도구들 only 파이썬 목표 참고 : 여기 이진탐색 연습하기 이건 그냥 외우자 .매우 좋...
#LeetCode#Naver Blog
#LeetCode #개발자의도구들
- only 파이썬
- 목표 참고 : 여기
이진탐색 연습하기
LeetCode(esay 35. search Insert Position) 48.2%
⚠️ 문제조건: O(log n)
target이 발견되는 index를 reutrn
없다면, 어떤 index에 넣어야하는지 return
-- 배열의 모든 수는 겹치지 않는다.
이분탐색으로 풀면된다.
class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
start = 0
end = len(nums) - 1
while start <= end:
mid = (start + end ) // 2
if nums[mid] < target:
start = mid + 1
else:
end = mid - 1
return start
- 이건 그냥 외우자
- .매우 좋은 글이 있어서 참고하면 좋으나, 이해가 잘 안가서 머리 쌩쌩할 때 읽어보기
- https://www.acmicpc.net/blog/view/109
암기법
나는 천재가 아니닌깐 그냥 외우자
start <= end
=이거를 - - 로 생각하고
+
-
붙어야 된다로 인식하면 외우기 쉽다.
start + 1
end - 1