【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 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的更多相关文章
- 【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 ...
- 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现
一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...
- 【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 ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告
今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 找到旋转后有序数组中的最小值
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4032570.html 原题: Suppose a sorted array is ...
- 【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 ...
- 【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 ...
- 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...
- 【Leetcode】81. Search in Rotated Sorted Array II
Question: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? ...
随机推荐
- Ubuntu16.04/LinuxMint18安装openjdk-7-jdk
LinuxMint18的安装源已经默认没有openjdk7了,所以要自己手动添加仓库,如下: sudo add-apt-repository ppa:openjdk-r/ppa sudo apt-ge ...
- css3 input边框蓝光特效
1.style样式 <style type="text/css"> .demok3_input { transition: all 0.30s ease-in-out; ...
- rails
http://ruby-toolbox.com/ ~/.gemrc --- :backtrace: false :benchmark: false :bulk_threshold: 1000 :sou ...
- Angular.js通过bootstrap实现经典的表单提交
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel= ...
- MySQL5.0数据库的安装
======================= 未完待续,持续更新中... -------------------------------------------------
- WinForm窗体代码结构优化
选择系统新建WinForm程序,会生成FormMain.cs/ FormMain.Designer.cs/ FormMain.resx 当我们发现系统生成的FormMain.cs里面代码太多的时候,不 ...
- linux下常见解压缩命令
linux下常见的压缩文件格式有tar.gz.tar.gz.tar.bz2.zip等等.对于不同的压缩文件格式有对应的解压缩命令.下面就对此小结一下: 1.后缀为.tar 用 tar –xvf 解压 ...
- ARCGIS9.3安装说明
1) 安装LMSetup.exe" 其中第一步选择第一项,并使用"37102011.efl9"文件做为lic文件,在使用之前需要将该文件中的主机名改为本机的机器名, ...
- Node.js入门笔记(1):基本概念
Node.js和JavaScript: 核心都是ECMAScrit,比如数据类型,语法结构,内置对象等等. 但是在js中顶层是window 在node中的不存在这个window(console.log ...
- Debugging D Program on Windows
http://x64dbg.com/ http://www.ollydbg.de/version2.html