알고리즘
[LeetCode] # 35. Search Insert Position
유병각
2022. 4. 24. 15:10
35. Search Insert Position
풀이_#1
이분탐색 사용
class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
low = 0
high = len(nums) - 1
while (low <= high):
mid = low + (high - low)// 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
low = mid + 1
elif nums[mid] > target:
high = mid - 1
return low