leetcode 153

旋转过的严格递增列表, 求其中的最小值.

不难, 注意判断边界条件就可以, 算法复杂度应该是O(logn)

class Solution:
    def findMinInRange(self, nums: List[int], start, end) -> int:
        if start == end:
            return nums[start]
        if nums[start] < nums[end]:
            return nums[start]
        while start < end:
            mid = (start + end) // 2
            if nums[start] > nums[mid]:
                end = mid
                continue
            elif nums[start] == nums[mid]:
                # assert start + 1 == end
                return min(nums[start], nums[end])
            else:
                start = mid
                continue
    def findMin(self, nums: List[int]) -> int:
        len_nums = len(nums)
        result = self.findMinInRange(nums, 0, len_nums-1)
        return result
posted @ 2021-11-21 18:59  茫茫碧落  阅读(33)  评论(0编辑  收藏  举报