일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Protocol
- LeetCode
- DWG
- 운영체제
- 109. Convert Sorted List to Binary Search Tree
- 프로그래머스
- 715. Range Module
- 315. Count of Smaller Numbers After Self
- Python
- Substring with Concatenation of All Words
- Convert Sorted List to Binary Search Tree
- attribute
- data science
- Python Implementation
- Generator
- concurrency
- kaggle
- 파이썬
- iterator
- t1
- 시바견
- Python Code
- 30. Substring with Concatenation of All Words
- Regular Expression
- 43. Multiply Strings
- shiba
- Class
- 밴픽
- Decorator
- 컴퓨터의 구조
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 |