33. Search in Rotated Sorted Array
Medium
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1Input: nums = [1], target = 0
Output: -1Last updated
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1Input: nums = [1], target = 0
Output: -1Last updated
class Solution:
def search(self, nums: List[int], target: int) -> int:
pivot = self.findPivot(nums, target)
return pivot
def findPivot(self, nums, target):
low = 0
high = len(nums)-1
while low <= high:
mid = low+(high-low)//2
if nums[mid] == target:
return mid
elif nums[low] <= nums[mid]:
if nums[low] <= target <= nums[mid]:
high = mid - 1
else:
low = mid + 1
else:
if nums[mid] <= target <= nums[high]:
low = mid + 1
else:
high = mid - 1
return -1