zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    正则表达式常用公式
    造轮子之--Redis
    SqlServer 查询计划分析
    实现poster,json,base64等编码转码工具
    win nginx + php bat启动/停止脚本
    php设计模式——单例模式
    [php]php设计模式 (总结)
    升级openssl 操作记录
    PHP URL安全的Base64位编码
    php curl使用 常用操作
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12773966.html
Copyright © 2011-2022 走看看