leetcode153. Find Minimum in Rotated Sorted Array

Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = 0,1,2,4,5,6,7 might become:

复制代码
[4,5,6,7,0,1,2] if it was rotated 4 times.
[0,1,2,4,5,6,7] if it was rotated 7 times.

Notice that rotating an array a\[0, a1, a2, ..., an-1] 1 time results in the array a\[n-1, a0, a1, a2, ..., an-2].

Given the sorted rotated array nums of unique elements, return the minimum element of this array.

You must write an algorithm that runs in O(log n) time.

Example 1:

Input: nums = 3,4,5,1,2

Output: 1

Explanation: The original array was 1,2,3,4,5 rotated 3 times.

Example 2:

Input: nums = 4,5,6,7,0,1,2

Output: 0

Explanation: The original array was 0,1,2,4,5,6,7 and it was rotated 4 times.

Example 3:

Input: nums = 11,13,15,17

Output: 11

Explanation: The original array was 11,13,15,17 and it was rotated 4 times.

Constraints:

复制代码
n == nums.length
1 <= n <= 5000
-5000 <= nums[i] <= 5000
All the integers of nums are unique.
nums is sorted and rotated between 1 and n times.

https://leetcode.cn/problems/find-minimum-in-rotated-sorted-array/description/

思路:简洁二分法

python 复制代码
class Solution:
    def findMin(self, nums: List[int]) -> int:
        left, right = -1, len(nums) - 1  # 开区间 (-1, n-1)
        while left + 1 < right:  # 开区间不为空
            mid = (left + right) // 2
            if nums[mid] < nums[-1]:
                right = mid
            else:
                left = mid
        return nums[right]
相关推荐
csdn_aspnet2 小时前
javascript 算法 LeetCode 编号 70 - 爬楼梯
开发语言·javascript·算法·leetcode·ecmascript
shehuiyuelaiyuehao2 小时前
多线程入门
java·python·算法
Navigator_Z2 小时前
LeetCode //C - 1073. Adding Two Negabinary Numbers
c语言·算法·leetcode
醇氧3 小时前
【OpenClaw】更换阿里百炼完整配置指南
算法·ai
Tina学编程3 小时前
[HOT100]每日一练------最长连续序列
算法·hot 100
csdn_aspnet3 小时前
PHP 算法 LeetCode 编号 70 - 爬楼梯
算法·leetcode·php
沈浩(种子思维作者)3 小时前
没有错误,正确将一文不值
人工智能·python·算法·量子计算
x_xbx3 小时前
LeetCode:5. 最长回文子串
算法·leetcode·职场和发展
快手技术3 小时前
免费报名|生成式推荐技术如何实现体系化演进?快手技术沙龙第四期开启!
算法
初夏睡觉3 小时前
数字截断求和 题解
算法