classSolution(object):
defsearch(self, nums, target):
ifnot nums: return -1
n = len(nums)
left, right = 0, n-1while left<right:
mid = left + (right-left)//2if nums[mid]==target:
return mid
elif nums[left]<=nums[mid]:
if nums[left]<=target<nums[mid]:
right = mid-1else:
left = mid+1else:
if nums[mid]<target<=nums[right]:
left = mid+1else:
right = mid-1return left if nums[left]==target else -1
classSolution(object):
defsearchInsert(self, nums, target):
n = len(nums)
left, right =0, n-1if nums[left]>target: return left
if nums[right]<target: return right+1while left<right:
mid = left + (right-left)//2if nums[mid] == target:
return mid
elif nums[mid]<target:
left = mid+1else:
right = mid
return left
classSolution(object):
defsearch(self, nums, target):
ifnot nums: returnFalse
n = len(nums)
left, right = 0, n-1while left<=right:
mid = left + (right-left)//2if nums[mid]==target:
returnTrueif nums[mid]==nums[left]==nums[right]:
left += 1
right -= 1elif nums[left]<=nums[mid]:
if nums[left]<=target<nums[mid]:
right = mid - 1else:
left = mid + 1else:
if nums[mid]<target<=nums[right]:
left = mid + 1else:
right = mid -1returnFalse
classSolution(object):
deffindMin(self, nums):
ifnot nums: return
n = len(nums)
left, right = 0, n-1while left<right:
while left<right and nums[left]==nums[left+1]: left += 1while left<right and nums[right]==nums[right-1]: right -= 1
mid = left + (right-left)//2if nums[mid]>nums[right]:
left = mid + 1else:
right = mid
return nums[left]
classSolution(object):
deffindPeakElement(self, nums):
ifnot nums: return
n = len(nums)
left, right = 0, n-1while left<right:
mid = left + (right-left)//2if nums[mid]<nums[mid+1]:
left = mid+1else:
right = mid
return left
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】博客园携手 AI 驱动开发工具商 Chat2DB 推出联合终身会员
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个超经典 WinForm,WPF 卡死问题的终极反思
· ASP.NET Core - 日志记录系统(二)
· .NET 依赖注入中的 Captive Dependency
· .NET Core 对象分配(Alloc)底层原理浅谈
· 聊一聊 C#异步 任务延续的三种底层玩法
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(一):从.NET IoT入
· .NET 开发的分流抢票软件,不做广告、不收集隐私
· 一个超经典 WinForm,WPF 卡死问题的终极反思
· 开箱你的 AI 语音女友「GitHub 热点速览」
· 前端实现 HTML 网页转 PDF 并导出