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]
相关推荐
专注仿真3 小时前
仿真智能体训练(评估篇):基于蒙特卡洛批量对抗的评估算法详解与实战
算法·仿真·评估·afsim智能体·仿真训练
小O的算法实验室8 小时前
SEVC,城市灾害环境下应急物流的帕累托归档引导式多目标路径规划
算法
维克兜率天8 小时前
【维克】布林带:通道类指标的“量化基石“
人工智能·python·算法
风落青芜8 小时前
C++入门:类和对象(二)
开发语言·数据结构·c++·算法
wuyk5559 小时前
AI 时代的嵌入式该怎么学-11-C不是语法是对内存的显式建模
c语言·开发语言·stm32·单片机·嵌入式硬件·mcu·算法
吞下星星的少年·-·9 小时前
LeetCode 热题 100 最长连续序列(哈希表)
算法·leetcode·散列表
风落青芜10 小时前
C++入门:类和对象(三)
开发语言·数据结构·c++·算法
Pniubi11 小时前
力扣25.K个一组反转链表
算法·leetcode·链表
16月6日-晴11 小时前
力扣22题,括号生成
算法