# 81. 搜索旋转排序数组 II

输入：nums = [2,5,6,0,0,1,2], target = 0



输入：nums = [2,5,6,0,0,1,2], target = 3

• 1 <= nums.length <= 5000
• -104 <= nums[i] <= 104
• 题目数据保证 nums 在预先未知的某个下标上进行了旋转
• -104 <= target <= 104

• 这是 搜索旋转排序数组 的延伸题目，本题中的 nums  可能包含重复元素。
• 这会影响到程序的时间复杂度吗？会有怎样的影响，为什么？

0

class Solution:
def search(self, nums: List[int], target: int) -> bool:
nums=list(set(nums))
left=0
right=len(nums)-1
while left<right:
mid=(right-left)//2+left
if nums[mid]==target:
return True
if nums[mid]>nums[left]:
if nums[left]<=target<=nums[mid]:
right=mid
else:
left=mid+1
else:
if nums[mid+1]<=target<=nums[right]:
left=mid+1
else:
right=mid

if target==nums[left]:
return True
else:
return False

1

class Solution:
def search(self, nums: List[int], target: int) -> bool:
left=0
right=len(nums)-1
while right-left>=2:
mid=(right-left)//2+left
if nums[mid]==target:
return True
if nums[left]==nums[right]:
left+=1
continue
#left part is ordered
if nums[left] <= nums[mid]:
#target between start:mid
if nums[left] <= target < nums[mid]:
right = mid-1
#target between mid:end
else:
left = mid+1
#right part is ordered
else:
# target between mid:end
if nums[mid] < target <= nums[right]:
left =mid+1
# target between start:mid
else:
right = mid-1

for n in nums[left:right+1]:
if n==target:
return True
return False

posted @ 2021-04-08 08:36  XXXSANS  阅读(38)  评论(0编辑  收藏  举报