일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- LeetCode
- shiba
- Decorator
- t1
- Regular Expression
- 315. Count of Smaller Numbers After Self
- attribute
- DWG
- 파이썬
- data science
- 프로그래머스
- 시바견
- concurrency
- iterator
- 운영체제
- Generator
- kaggle
- Python Code
- 컴퓨터의 구조
- Python
- 밴픽
- 43. Multiply Strings
- Class
- Protocol
- Convert Sorted List to Binary Search Tree
- 109. Convert Sorted List to Binary Search Tree
- 30. Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- Python Implementation
- 715. Range Module
Archives
- Today
- Total
Scribbling
34. Find First and Last Position of Element in Sorted Array 본문
Computer Science/Coding Test
34. Find First and Last Position of Element in Sorted Array
focalpoint 2021. 9. 11. 22:31class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
return self.binary_search(nums, 0, len(nums)-1, target)
def binary_search(self, nums, left, right, target):
if left > right:
return [-1, -1]
mid = (left + right) // 2
mid_val = nums[mid]
if mid_val > target:
return self.binary_search(nums, left, mid-1, target)
elif mid_val < target:
return self.binary_search(nums, mid+1, right, target)
else:
i1, i2 = self.binary_search(nums, left, mid-1, target)
i3, i4 = self.binary_search(nums, mid+1, right, target)
if i1 == -1 and i4 == -1:
return [mid, mid]
if i1 == -1 and i4 != -1:
return [mid, i4]
if i1 != -1 and i4 == -1:
return [i1, mid]
return [i1, i4]
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 43. Multiply Strings (0) | 2021.09.13 |
---|---|
LeetCode: 42. Trapping Rain Water (0) | 2021.09.12 |
33. Search in Rotated Sorted Array (0) | 2021.09.11 |
LeetCode: 31. Next Permutation (0) | 2021.09.09 |
35. Search Insert Position (0) | 2021.09.08 |