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