AcWing 22. 旋转数组的最小数字--一种Accept但是实际错误的解法
原题链接
中等
作者:
roon2300
,
2021-04-13 16:22:19
,
所有人可见
,
阅读 343
def solve_1(arr, hh, tt):
while hh < tt and arr[hh] <= arr[tt]: tt -= 1
if arr[hh] < arr[tt]: return arr[hh]
while hh < tt:
mid = (hh + tt) >> 1
if arr[mid] < arr[0]:
tt = mid
else:
hh = mid + 1
return arr[hh]
def solve_2(arr, hh, tt):
while hh < tt and arr[hh] <= arr[tt]: tt -= 1
while hh < tt:
mid = (hh + tt) >> 1
if arr[hh] < arr[tt]: return arr[hh]
if arr[mid] < arr[hh]:
tt = mid
else:
hh = mid + 1
return arr[hh]
def solve_wrong(arr, hh, tt):
if arr[hh] < arr[tt]: return arr[hh]
while hh < tt:
mid = (hh + tt) >> 1
if arr[hh] == arr[mid]:
return min(arr)
elif arr[hh] < arr[mid]:
hh = mid + 1
else:
tt = mid
return arr[hh]
solve = solve_1
solve = solve_2
# solve = solve_wrong
class Solution:
def findMin(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums: return -1
n = len(nums)
if n == 1: return nums[0]
return solve(nums, 0, n - 1)