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], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-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]
相关推荐
小袁顶风作案5 分钟前
leetcode力扣——135.分发糖果
算法·leetcode·职场和发展
于齐龙7 分钟前
2025年12月24日 - 数据结构
数据结构
橘颂TA15 分钟前
【Linux】从 “抢资源” 到 “优雅控场”:Linux 互斥锁的原理与 C++ RAII 封装实战(Ⅰ)
linux·运维·服务器·c++·算法
YGGP31 分钟前
【Golang】LeetCode 19. 删除链表的倒数第 N 个节点
算法·leetcode·链表
池塘的蜗牛37 分钟前
mmse-based-OFDM-signal-processing(2)
算法
Kris_LinSD1 小时前
算法小实验——分治算法快速排序问题实验(含报告)
c语言·算法
Super小白&1 小时前
十大经典排序算法详解(附C语言实现+复杂度分析)
c语言·算法·排序算法
Eloudy1 小时前
Birkhoff 多胞形,双随机矩阵的几何世界
算法
2503_946971861 小时前
【SystemDesign/HA】2025年度高可用分布式仿真节点与预测模型容灾演练配置 (Disaster Recovery Config)
大数据·分布式·算法·系统架构·数据集