일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- kaggle
- 30. Substring with Concatenation of All Words
- 파이썬
- Protocol
- iterator
- t1
- 밴픽
- DWG
- Decorator
- LeetCode
- Generator
- 시바견
- Convert Sorted List to Binary Search Tree
- concurrency
- 프로그래머스
- Substring with Concatenation of All Words
- 43. Multiply Strings
- 715. Range Module
- Class
- 컴퓨터의 구조
- Python Code
- data science
- Python
- attribute
- Python Implementation
- 109. Convert Sorted List to Binary Search Tree
- Regular Expression
- 운영체제
- 315. Count of Smaller Numbers After Self
- shiba
Archives
- Today
- Total
Scribbling
35. Search Insert Position 본문
Binary Search 살짝만 바꿔주자
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
return self.binary_search(nums, 0, len(nums)-1, target)
def binary_search(self, nums, l, r, target):
if l == r:
if target <= nums[l]:
return l
else:
return l + 1
mid_idx = (l + r) // 2
mid = nums[mid_idx]
if target <= mid:
return self.binary_search(nums, l, mid_idx, target)
else:
return self.binary_search(nums, mid_idx+1, r, target)
'Computer Science > Coding Test' 카테고리의 다른 글
33. Search in Rotated Sorted Array (0) | 2021.09.11 |
---|---|
LeetCode: 31. Next Permutation (0) | 2021.09.09 |
LeetCode: 49. Group Anagrams (0) | 2021.09.08 |
LeetCode: 41. First Missing Positive (0) | 2021.09.08 |
LeetCode: 47. Permutations II (0) | 2021.09.07 |