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]
相关推荐
阿里技术5 小时前
Agent 评测:方法论与体系设计
大数据·人工智能·算法
cxr8285 小时前
大语言模型上下文缓存命中率测试全场景清单
人工智能·python·算法·缓存·语言模型·自然语言处理·llm
noipp5 小时前
推荐题目:洛谷 P13554 【MX-X15-T1】奶龙龙
c语言·数据结构·c++·算法·编程·洛谷
念风5 小时前
一阶低通滤波器系数的两种计算方法
算法
geovindu6 小时前
go: Enumeration Algorithm
开发语言·后端·算法·golang·枚举算法
退休倒计时6 小时前
【每日一题】LeetCode 287. 寻找重复数 TypeScript
算法·leetcode·typescript
奋发向前wcx6 小时前
y1,y2总复习笔记2 2026.7.15
java·笔记·算法
AI科技星6 小时前
《全域数学·工程应用大典》113–200讲完整总目录
数据结构·人工智能·算法·机器学习·线性回归·乖乖数学·全域数学
ttod_qzstudio6 小时前
【软考算法】软件设计师下午第四题算法总纲:从“直接放弃“到“稳稳拿分“
算法·软考
广州灵眸科技有限公司7 小时前
xfce桌面旋转说明:基于灵眸科技EASY-EAI-Nano-TB
网络·网络协议·tcp/ip·算法·php