• Leetcode练习(Python):数组类:第153题:假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 请找出其中最小的元素。 你可以假设数组中不存在重复元素。


    题目:
    假设按照升序排序的数组在预先未知的某个点上进行了旋转。  ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。  请找出其中最小的元素。  你可以假设数组中不存在重复元素。 
    思路:
    思路1,用函数,这样感觉有点不厚道;思路2,用二分法
    程序1:
    class Solution:
        def findMin(self, nums: List[int]) -> int:
            nums.sort()
            output = nums[0]
            return output
    程序2:
    class Solution:
        def findMin(self, nums: List[int]) -> int:
            length = len(nums)
            if length <= 0:
                return 0
            if length == 0:
                return nums[0]
            head = 0
            tail = length - 1
            while head < tail:
                middle = (head + tail) // 2
                if nums[middle] > nums[tail]:
                    head = middle + 1
                else:
                    tail = middle
            output = nums[head]
            return output
  • 相关阅读:
    在中文版VS2008中安装MVC
    【原创】最优惠的企业邮局
    【推荐】双模虚拟主机 WINDOWS经济型或 UNIX经济型
    CSS基础
    第一篇文章
    一道面试题
    IIS做web server有些中文名文件不能下载
    偶遇指间流沙
    迷失的女孩
    身边的小故事二则
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12773966.html
Copyright © 2020-2023  润新知