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]
相关推荐
无限码力几秒前
携程0510笔试真题【单数组交换】
算法·携程笔试·携程笔试真题·携程0510笔试真题
Love_云宝儿40 分钟前
WKT数据示例并与GeoJSON数据对比
数据结构·gis
BlockWay1 小时前
WEEX Labs 周度观察:微软-OpenAI 合作调整与AI 多云趋势
大数据·人工智能·算法·安全·microsoft
风筝在晴天搁浅1 小时前
快手 CodeTop LeetCode 224.基本计算器
数据结构·算法·leetcode
Smoothcloud润云1 小时前
5大功能精修,重构AI算力使用体验!
java·人工智能·windows·算法·重构·编辑器·sublime text
计算机安禾1 小时前
【算法分析与设计】第41篇:确定性与非确定性多项式时间:P与NP的形式化
算法
牢姐与蒯2 小时前
c++数据结构之c++11(一)
数据结构·c++
iiiiyu2 小时前
IO流(二)
java·开发语言·数据结构·编程语言
leo__5202 小时前
随机接入退避算法过程模拟实现
网络·算法
-To be number.wan2 小时前
算法日记 | STL- sort排序
c++·算法