728x90
문제
https://leetcode.com/problems/search-in-rotated-sorted-array-ii/
Search in Rotated Sorted Array II - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
내 풀이
from collections import deque
class Solution:
def search(self, nums: List[int], target: int) -> bool:
nums = deque(set(nums))
last = nums[-1]
while nums:
if nums[0] <= last:
break
nums.rotate(-1)
left,right = 0, len(nums) - 1
while(left <= right):
pivot = left + (right - left) // 2
if nums[pivot] == target:
return True
elif nums[pivot] < target:
left = pivot + 1
else:
right = pivot - 1
return False
알게 된 것
deque().rotate(-1) : deque의 원소를 -1만큼 회전한다.
binary search 의 python version을 외운듯 하다
피드백
'프로그래밍 > leetcode' 카테고리의 다른 글
[leetcode] Top K Frequent Elements (0) | 2022.04.09 |
---|---|
[leetcode] Search a 2D matrix (0) | 2022.03.31 |
[leetcode] Two City Scheduling (0) | 2022.03.25 |
[leetcode] Smallest String With A Given Numeric Value (0) | 2022.03.22 |
[leetcode] 1007. Minimum Domino Rotations For Equal Row (0) | 2022.03.21 |