이 문제도 이진탐색을 이용하면 쉽게! 해결된다.
3분컷
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
# Binary Search
low = 0
high = len(nums) - 1
while low <= high:
mid = (low + high) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
low = mid + 1
elif nums[mid] > target:
high = mid - 1
return low
성공이다.
오늘도 한 개 했다. 뿌듯
'Leetcode' 카테고리의 다른 글
Leetcode 167. Two Sum II - Input array is sorted (0) | 2021.10.07 |
---|---|
Leetcode 283. Move Zeroes (0) | 2021.10.06 |
Leetcode 189. Rotate Array (0) | 2021.10.05 |
Leetcode 977. Squares of a Sorted Array - Two Pointers (0) | 2021.10.04 |
Leetcode 278. First Bad Version - Binary Search Tree (0) | 2021.10.02 |