[LeetCode 35. Search Insert Position] Easy 3/5 26/150 99.12%
Algorithm/LeetCode 문제 풀이 2019. 5. 16. 06:07바이너리 서치에 대해서 배웠습니다.
생각보다 간단하네요 ㅎㅎ
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
if not nums:
return 0
def search(nums, target, l, r):
while l <= r:
m = (l+r)//2
if nums[m] == target:
return m
if nums[m] < target:
l = m+1
else:
r = m-1
return l
return search(nums, target, 0, len(nums)-1)
'Algorithm > LeetCode 문제 풀이' 카테고리의 다른 글
AtCoder Beginner Contest 194-c (0) | 2021.03.10 |
---|---|
[LeetCode 70. Climbing Stairs] Easy (0) | 2019.06.10 |
[LeetCode 74. Search a 2D Matrix] 2/5 25/150 (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 |