题目概述:


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.

解题思路:

在一个有顺序的数组中查找个最小值,很容易想到的就是二分法,的确,这里用的就是二分,不过要做一点改进。
改进的思路是这样的:
1.我们容易想到最好的情况就是这个数组是普通的排好序的,如【1,2,3,4】,那么最小的必然是下标最小的。用旋转的情况假设第一个小于最后一个那么最小值肯定也是第一个,也就是说计算过程中只要找到left<right的结果就出来了 2、如果这里面出现了rotate的话,如【3,4,1,2】那么最小值肯定会出现在左右的一遍,那么到底是哪边呢,这里我们可以比较最左和最后值的大小,比如这里`3="">2说明在这里前面的那个3是旋转过去的。这是后我们就要用中间那个值,这里是(0+3)/2=1`这里4>2(right)说明最小值应该实在后半块里的,于是乎改left就好。反之是改right就是一样的过程了,就是一个二分的思想

 class Solution:
# @param num, a list of integer
# @return an integer
def findMin(self, num):
l = len(num)
left = 0
right = l-1
while num[left] > num[right]:
mid = (left +right)/2
if num[mid] < num[right]:
right = mid
else :
left = mid + 1
return num[left] a = [3,0,1,2]
#a = [4, 5, 6, 7, 8, 9, 18,19, 0, 1, 2, 3]
s = Solution()
print s.findMin(a)

加深问题:Find Minimum in Rotated Sorted Array II

题目描述:

跟上面的一样,唯一改变的是现在数字可以重复了

解题思路:


这个看起来改了一点,实际上直接用上面的方法就很难得出结果里,leetcode给这个的评级是hard,但是我开始实在是想不出有什么好办法,后来发现解法是:对于相等的存在无法判断只能暴力-_-#。不过我的代码有点丑陋,直接上一题加的,改了个特殊情况,leetcode的标程比这个好看些,不过效率差不多。

 class Solution:
# @param num, a list of integer
# @return an integer
def findMin(self, num):
l = len(num)
left = 0
right = l-1
while num[left] >= num[right]:
mid = (left +right)/2
if num[mid] < num[right]:
right = mid
elif num[left] > num[right]:
left = mid + 1
else :
if right == left:
return num[left]
res = 10000
for i in xrange(0,l):
if num[i] < res:
res = num[i]
return res
return num[left] a = [2,0,1,1,1]
#a = [3,4, 5, 6, 7, 8, 9, 18,19, 0, 1, 2, 3]
s = Solution()
print s.findMin(a)

【leetcode】Find Minimum in Rotated Sorted Array I&&II的更多相关文章

  1. 【leetcode】Find Minimum in Rotated Sorted Array I & II (middle)

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

  2. 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现

    一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...

  3. 【LeetCode】Find Minimum in Rotated Sorted Array 在旋转数组中找最小数

    Add Date 2014-10-15 Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some p ...

  4. 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告

    今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...

  5. 【LeetCode】Find Minimum in Rotated Sorted Array 找到旋转后有序数组中的最小值

     本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4032570.html 原题: Suppose a sorted array is ...

  6. 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  7. 【LeetCode】33. Search in Rotated Sorted Array (4 solutions)

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  8. 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...

  9. 【Leetcode】81. Search in Rotated Sorted Array II

    Question: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? ...

随机推荐

  1. C语言拾遗(一)

    越来越体会到C语言的重要性,不管是在计算机底层的理解上,还是在算法数据结构上,所以遂决定重新拾起C语言,不定期更新一些知识点. 推荐博客:http://blog.csdn.net/itcastcpp ...

  2. bzoj2091【Poi2010】The Minima Game

    直接dp就好了 每个人肯定会去选最大的,用dp[i]表示选了后i个点时先手-后手的最大值(因为从后往前扫才好转移啊 QwQ~) dp[i]=max(c[j]-dp[j-1]),(j<=i) 直接 ...

  3. Bzoj1597 [Usaco2008 Mar]土地购买

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4005  Solved: 1460 Description 农夫John准备扩大他的农场,他正在考虑N ...

  4. backup mysql

    #!/bin/bashcd /home/Licw/backup_openDBNow=$(date +"%m-%d-%Y--%H:%M:%S")#echo $NowFile=$Now ...

  5. RedHat的定制安装

    1.前提是先安装好VMware,接着打开VMware软件,选择新进虚拟机.选择Custom 2.选择iso --使用镜像文件安装. 3.选择操作系统和版本. 4.给产生的配置的文件给放在你新建的文件夹 ...

  6. 正则表达式之g标志,match和 exec

    1.g标志    g标志一般是与match和exec来连用,否则g标志没有太大的意义. 先来看一个带g标志的例子: var str = "tankZHang (231144) tank yi ...

  7. mui jquery 同时使用

    (function ($, doc, $$) { $.init(); $.ready(function () { var cityPicker = new $.PopPicker({ layer: } ...

  8. rpm---linux软件安装与管理

    linux的安装命令选项太多,整理一下,方便后期查找. 汇总: install: rpm -ivh 包全名 安装 upgrade: rpm -Uvh 包全名 升级 erase: rpm -e 包名 删 ...

  9. 2015.4.21 实现一般免登陆,微博QQ分享,字体自适应等

    1.实现一般的登录验证和免登陆: 解决方法:node方法代码,nodeJS实现的session模块,不完整,但能用,仅供参考. 语言无所谓,session的机制都是一样的,实现不一样而已,:   2. ...

  10. 一段freemarker高级分页效果的代码

    <a onclick="page(1)">首页</a> [#if currpage != 1] [#assign last=currpage - 1] &l ...