- JY:pivot 有序数组(值可重复)中的查找数值
1、二分查找
- 该题的任何解法同样可用于 0033_search-in-rotated-sorted-array【M】中
nums
可能包含重复元素,这会影响到程序的时间复杂度吗?- 会,使用二分查找局部有序时,当
nums[mid] == nums[low]
时(或其它类似情况),无法确定左侧区间还是右侧区间是升序区间,只能low
左移一位后继续判断。 - 该题在 0033_search-in-rotated-sorted-array【M】的基础上增加了重复的数字,当左端点和右端点(或中间值)相等时,无法判断
mid
的左半边还是右半边才是有序数组,可以在两端不断去除重复的元素后再进一步判断。
- 基于
low
和high
去除重复元素(否则后续可能出现nums[low] == nums[mid] == nums[high]
,使得无法定位哪部分区间有序),随后基于low
和mid
的值确定哪部分属于有序区间,再结合target
所处区间判断更新low
还是high
:
-
- 如果
nums[mid] == target
,则直接返回 - 如果
nums[low] <= nums[mid]
,表明左侧区间有序;如果target
在左侧区间,则更新high = mid - 1
- 如果
nums[low] > nums[mid]
,表明右侧区间有序;如果target
在右侧区间,则更新low = mid + 1
- 如果
from typing import List, Dict
class Solution:
def search(self, nums: List[int], target: int) -> bool:
low, high = 0, len(nums) - 1
# jy: 当左右两端数值相等时, 移动 low 或 high, 直到两端数值不等
# (注意: low 和 high 不能同时移动, 否则重复的元素可能全部被清除)
while low < high and nums[low] == nums[high]:
# low += 1
high -= 1
while low <= high:
mid = low + (high - low) // 2
if nums[mid] == target:
return True
if 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 False
nums = [2, 5, 6, 0, 0, 1, 2]
target = 3
res = Solution().search(nums, target)
print(res)
2、改写解法 1
- 优先判断
mid
和high
的值,确定左侧还是右侧区间属于有序区间;再判断target
是在左侧区间还是右侧区间,进而更新low
或high
from typing import List, Dict
class Solution:
def search(self, nums: List[int], target: int) -> bool:
if not nums:
return -1
low, high = 0, len(nums) - 1
while low <= high:
mid = low + (high - low) // 2
if nums[mid] == target:
return True
# jy: 如果 low 和 high 相等, 则移动 low 或 high (注意不能同时移
# 动, 否则可能把重复的元素全部去除, 一个都不保留)
if nums[low] == nums[high]:
low += 1
#high -= 1
continue
if nums[mid] <= nums[high]:
if nums[mid] < target <= nums[high]:
low = mid + 1
else:
high = mid - 1
else:
if nums[low] <= target < nums[mid]:
high = mid - 1
else:
low = mid + 1
return False
nums = [1, 0, 1, 1, 1]
target = 0
res = Solution().search(nums, target)
print(res)
3、修改解法 1 的去重逻辑
from typing import List, Dict
class Solution:
def search(self, nums: List[int], target: int) -> bool:
low, high = 0, len(nums) - 1
while low <= high:
mid = low + (high - low) // 2
if nums[mid] == target:
return True
# jy: 当 mid、low、high 的值均相等时, 同时移动 low 和 high 也不
# 会使得重复元素全部被清除 (至少还保留了 mid 位置的值)
if nums[mid] == nums[low] == nums[high]:
low += 1
high -= 1
continue
if 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 False
nums = [1, 0, 1, 1, 1]
target = 0
res = Solution().search(nums, target)
print(res)
4、改写解法 3
- 优先基于
mid
和high
的值判断升序区间,随后判断target
值是否位于升序区间,从而确定更新low
还是high
。 - 其实这道题只需根据 0033_search-in-rotated-sorted-array【M】的搜索改变一点判断就行,即当
nums[low] == nums[mid] == nums[high]
时不知道该怎么移动,此时low += 1
,high -= 1
后再去判断即可
from typing import List, Dict
class Solution:
def search(self, nums, target):
low, high = 0, len(nums) - 1
while low <= high:
mid = (low + high) // 2
if nums[mid] == target:
return True
if nums[low] == nums[mid] == nums[high]:
low += 1
high -= 1
continue
if nums[mid] <= nums[high]:
if nums[mid] < target <= nums[high]:
low = mid + 1
else:
high = mid - 1
else:
if nums[low] <= target < nums[mid]:
high = mid - 1
else:
low = mid + 1
return False
nums = [1, 0, 1, 1, 1]
target = 0
res = Solution().search(nums, target)
print(res)
标签:high,Search,target,nums,Rotated,mid,II,low,区间
From: https://blog.csdn.net/m0_66491750/article/details/140265745