Suppose a sorted array 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.

在任何一个sublist中,如果头元素大于尾元素,那么这个minimum一定在这个sublist中间的某一个位置。可以用二分法找到这个元素,复杂度是O(logN)。本题O(N)的算法也可以通过OJ,思路就是最简答的从头元素往后比,直到出现 nums[i] > nums[i+1]的情况,那么nums[i+1]就是Minimum element。

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

以下引用来自: http://bangbingsyb.blogspot.com/2014/11/leecode-find-minimum-in-rotated-sorted.html

和Search in Rotated Sorted Array II这题换汤不换药。同样当A[mid] = A[end]时,无法判断min究竟在左边还是右边。
 
3 1 2 3 3 3 3 
3 3 3 3 1 2 3
 
但可以肯定的是可以排除A[end]:因为即使min = A[end],由于A[end] = A[mid],排除A[end]并没有让min丢失。所以增加的条件是:
 
A[mid] = A[end]:搜索A[start : end-1]
 
 class Solution(object):
def findMin(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
left = 0
right = n-1
while left < right:
mid = (left+right)//2
if nums[mid] > nums[right]:
left = mid + 1
elif nums[mid] < nums[right]:
right = mid
else:
right -= 1 return nums[left]

Leetcode Find Minimum in Rotated Sorted Array I and II的更多相关文章

  1. Leetcode Find Minimum in Rotated Sorted Array 题解

    Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...

  2. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  3. [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  4. LeetCode Find Minimum in Rotated Sorted Array II

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...

  5. LeetCode Find Minimum in Rotated Sorted Array

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...

  6. Leetcode | Find Minimum in Rotated Sorted Array I && II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  7. LeetCode——Find Minimum in Rotated Sorted Array II

    Question Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allo ...

  8. LeetCode——Find Minimum in Rotated Sorted Array

    Description: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 ...

  9. [LeetCode] Find Minimum in Rotated Sorted Array 二分搜索

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. web cache server方案比较:varnish、squid、nginx

    linux运维中,web cache server方案的部署是一个很重要的环节,选择也有很多种比如:varnish.squid.nginx.下面就对当下常用的这几个web cache server做一 ...

  2. python实现一个图灵机器人

    这标题就是个噱头...其实用的别人的接口,就是这货. 下面是代码: # -*- coding: utf-8 -*- import urllib,urllib2 import sys import js ...

  3. asp.net sql 分页,,优化 排序 及分页,

    调用代码: <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix ...

  4. 截取视图某一段另存为部分视图(Partial View)

    在做ASP.NET MVC后台管理程序时,根据程序需要,Isus.NET需要实现一个功能,就是动态截取视图某一段另存为部分视图Partial View. 思路为在视图中,使用jQury的程序截图以及P ...

  5. C# 6.0

    C# 6.0 的新语法特性   回眸 C# 的前世今生 - 见证 C# 6.0 的新语法特性 序 目前最新的版本是 C# 7.0,VS 的最新版本为 Visual Studio 2017 RC,两者都 ...

  6. Linux 中 Weblogic 启动和关闭

    a.start weblogic1)找到 /Oracle/Middleware/user_projects/domains/ 用户_domain目录. nohup ./startWebLogic.sh ...

  7. sql基本命令

    --------------------------------------------------------SQL基本命令开始----------------------------------- ...

  8. 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum

    又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...

  9. SQL Server Data Tools – Business Intelligence for Visual Studio 2012安装时提示“The CPU architecture....”的解决方法

    SQL Server Data Tools – Business Intelligence for Visual Studio 2012,一个很强大的工具,下载地址:http://www.micros ...

  10. 20160205 - Windows 10 家庭版没有组策略

    问题描述:买笔电自带的正版系统,有一个需要使用gpedit.msc,发现并不存在. 解决办法:升级到 Windows 10 专业版