LeetCode153:Find Minimum in Rotated Sorted Array

本文介绍了一种算法,用于在未知旋转点的升序数组中查找最小元素。通过对比示例,如[3,4,5,1,2]和[4,5,6,7,0,1,2],展示了如何使用二分搜索法高效解决此问题。代码示例使用Python实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e.,  [0,1,2,4,5,6,7] might become  [4,5,6,7,0,1,2]).

Find the minimum element.

You may assume no duplicate exists in the array.

Example 1:

Input: [3,4,5,1,2] 
Output: 1

Example 2:

Input: [4,5,6,7,0,1,2]
Output: 0

LeetCode:链接

LeetCode变体:LeetCode154:Find Minimum in Rotated Sorted Array II

剑指Offer_编程题06:旋转数组的最小数字(指针)思路是一样的。这题因为没有重复数字,所以比剑指offer的题要简单一点。但是边界问题我实在是弄不清楚,所以就和剑指offer的题用了同样的代码。

class Solution(object):
    def findMin(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums:
            return None
        low = 0
        high = len(nums) - 1
        while low <= high:
            mid = (low + high) // 2
            if nums[mid] > nums[high]:
                low = mid + 1
            elif nums[mid] < nums[high]:
                high = mid
            else:
                high -= 1
        return nums[low]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值