【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? ...
随机推荐
- 用TTS实现文本转语音
最近被toefl单词虐成狗::>_<:: 想做一个可以自动把单词转成语音的软件,这样就可以在路上戴耳机边走边听啦~ 用微软的TTS语音库可以很容易地实现.早期的TTS要想实现中英文混合朗读 ...
- 配置maven
http://www.cnblogs.com/liunanjava/archive/2015/11/05/4936037.html
- 【转】HTML5的小知识点小集合
html5的小知识点小集合 html5知识 1. Doctype作用?标准模式与兼容模式各有什么区别? (1).<!DOCTYPE>声明位于位于HTML文档中的第一行,处于<h ...
- 面试题目——《CC150》数学与概率
面试题7.2:三角形的三个顶点上各有一只蚂蚁.如果蚂蚁开始沿着三角形的边爬行,两只或三只蚂蚁撞到一起的概率有多大?假定每只蚂蚁会随机选一个方向,每个方向被选到的几率相等,而且三只蚂蚁的爬行速度相同. ...
- [Unity3D]导入模型并且设置相应的属性
将资源拷贝到Assets中并设置 首先确保法线贴图的属性如下: 设置基本贴图和法线贴图 因为要发布到移动端,设置shader属性: 为了设置更好的反光效果,添加cubemap: 添加新的Materia ...
- centos7安装nexus私服2.14
今天项目经理叫搭个nexus私服,记录一下 nexus下载比较慢,可在本地下载,然后用sftp上传到linux系统去,下载地址https://www.sonatype.com/download-oss ...
- Fragment应用总结
1.FrameLayout 常用于作为Android自带组件的父节点 2.Fragment就是一个普通的Java类,用Android.app这个包 Fragment也是一个ListVi ...
- Java Web 学习链接
解决JSP中文乱码问题:http://www.cnblogs.com/chengkai/articles/2171848.html 编程思想之多线程与多进程:http://blog.csdn.net/ ...
- STM32f103之外部中断
一.背景 有个需求,IO口检测上升沿,然后做相应的动作.在此记录STM32F103的外部中断结构及配置方法, 以备下次快速上手使用. 有许多不太明白,又是老司机(:-D)帮忙,真的是站在别人的肩膀上会 ...
- Effective Java Second Edition --- Builder Pattern
如果类的构造器或者静态工厂中有多个参数,设计这种类时,Builder模式是一种不错的选择,特别是当大多数参数是可选的时候. 与使用传统的重载构造函数模式相比,使用Builder模式的客户端代码更易于阅 ...