gpt4 book ai didi

python - 在 O(log n) 时间内搜索旋转排序数组

转载 作者:太空狗 更新时间:2023-10-29 21:06:20 25 4
gpt4 key购买 nike

我在 this problem 上遇到了困难在 leetcode 上。

我不得不查找解决方案,因为出于某种原因,我的代码总是会出现一些问题。当在数组中查找不存在的目标数字时,我的当前代码仍然无限循环。

如果有更直观的方法来解决这个问题并帮助修复我的代码,我正在寻求一些帮助来理解。

我认为我不需要这一行:

if nums[mid] == target or nums[low] == target or nums[high] == target:
return target

我想知道我可以做些什么来确保如果我有一个包含 1-3 个数字的数组,我的代码可以找到目标而无需指定此条件语句。这里有几个例子

print(search([1, 2, 3], 1))
print(search([1], 1))
print(search([2, 1], 1))

还有一个像这样的例子 print(search([5, 1, 2, 3, 4], 6))我的代码从不返回 -1

def search(nums, target):
low = 0
high = len(nums)-1
while low <= high:
mid = (low + high) // 2
if nums[mid] == target or nums[low] == target or nums[high] == target:
return target
if nums[mid] <= nums[high]:
if target > nums[mid] and target <= nums[high]:
low = mid + 1
else:
high = mid - 1
elif nums[mid] > nums[low]:
if target >= nums[low] and target < nums[mid]:
high = mid - 1
else:
low = mid+1
return -1


print(search([1, 2, 3], 1))
print(search([5, 4, 1, 2, 3], 2))
print(search([3, 4, 5, 1, 2], 2))
print(search([1], 1))
print(search([2, 1], 1))
print(search([5, 1, 2, 3, 4], 6))

从遇到与我上面的解决方案类似的多个解决方案,人们说它是 O(logn) 但我不明白当我们将我们的降低 code> 和 high 相差 1。这让我相信解决方案是最坏情况 O(n)

寻求帮助!

最佳答案

这是一个稍微不同的版本

def search(nums, target):
low = 0
high = len(nums)-1

while low <= high:

mid = (low + high) // 2

l = nums[low]
m = nums[mid]
h = nums[high]

if target == l:
return low

if target == m:
return mid

if target == h:
return high

if any([
l < m < h and target < m,
l == m < h and target > m,
l > m < h and target > l and target > m,
l > m < h and target < l and target < m,
l < m > h and target > l and target < m
]):
high = mid

elif any([
l < m < h and target > m,
l > m < h and target > m and target < h,
l < m > h,
]):
low = mid

elif target < l or target > h:
break

elif l == m == h:
break

else:
raise Exception("This is not possible, only if some values are reverse/unordered!")

return -1

用这个数据测试(第一列是目标,第二列是列表,第三列是结果索引):

  -10 [1]                      -1
1 [1] 0
22 [1] -1
-10 [1, 2] -1
1 [1, 2] 0
2 [1, 2] 1
22 [1, 2] -1
-10 [2, 1] -1
1 [2, 1] 1
2 [2, 1] 0
22 [2, 1] -1
-10 [1, 5] -1
1 [1, 5] 0
5 [1, 5] 1
22 [1, 5] -1
-10 [5, 1] -1
1 [5, 1] 1
5 [5, 1] 0
22 [5, 1] -1
-10 [1, 2, 3] -1
1 [1, 2, 3] 0
2 [1, 2, 3] 1
3 [1, 2, 3] 2
22 [1, 2, 3] -1
-10 [3, 1, 2] -1
1 [3, 1, 2] 1
2 [3, 1, 2] 2
3 [3, 1, 2] 0
22 [3, 1, 2] -1
-10 [2, 3, 1] -1
1 [2, 3, 1] 2
2 [2, 3, 1] 0
3 [2, 3, 1] 1
22 [2, 3, 1] -1
-10 [1, 5, 10] -1
1 [1, 5, 10] 0
5 [1, 5, 10] 1
2 [1, 5, 10] -1
10 [1, 5, 10] 2
22 [1, 5, 10] -1
-10 [10, 1, 5] -1
1 [10, 1, 5] 1
5 [10, 1, 5] 2
2 [1, 5, 10] -1
10 [10, 1, 5] 0
22 [10, 1, 5] -1
-10 [5, 10, 1] -1
1 [5, 10, 1] 2
5 [5, 10, 1] 0
2 [1, 5, 10] -1
10 [5, 10, 1] 1
22 [5, 10, 1] -1
-10 [1, 2, 3, 4] -1
1 [1, 2, 3, 4] 0
2 [1, 2, 3, 4] 1
3 [1, 2, 3, 4] 2
4 [1, 2, 3, 4] 3
-10 [1, 2, 3, 4] -1
-10 [4, 1, 2, 3] -1
1 [4, 1, 2, 3] 1
2 [4, 1, 2, 3] 2
3 [4, 1, 2, 3] 3
4 [4, 1, 2, 3] 0
-10 [4, 1, 2, 3] -1
-10 [3, 4, 1, 2] -1
1 [3, 4, 1, 2] 2
2 [3, 4, 1, 2] 3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, 4, 5, 1] 2
5 [5, 77, 1, 2, 3] 0
77 [5, 77, 1, 2, 3] 1
1 [5, 77, 1, 2, 3] 2
2 [5, 77, 1, 2, 3] 3
3 [5, 77, 1, 2, 3] 4
5 [5, 6, 1, 2, 3] 0
6 [5, 6, 1, 2, 3] 1
1 [5, 6, 1, 2, 3] 2
2 [5, 6, 1, 2, 3] 3
3 [5, 6, 1, 2, 3] 4
5 [5, 6, 1, 2, 3, 4] 0
6 [5, 6, 1, 2, 3, 4] 1
1 [5, 6, 1, 2, 3, 4] 2
2 [5, 6, 1, 2, 3, 4] 3
3 [5, 6, 1, 2, 3, 4] 4
4 [5, 6, 1, 2, 3, 4] 5

之所以不是 O(n) 是因为在 O(n) 的情况下这意味着算法的性能会随着数据的增加而线性下降,而在这种情况下,随着输入数据的增加,性能会以对数方式下降,因为对于每次迭代,我们将数据集拆分为更小和更小的数据集。更小。

关于python - 在 O(log n) 时间内搜索旋转排序数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55405236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com