【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? ...
随机推荐
- JUnit备忘录
测试方法不应该有参数 使用junit做测试的时候发现总是报错:Method XXX should have no parameters; 后来发现是因为测试方法里面函数参数
- iOS多线程学习
在 iOS 中其实目前有 4 套多线程方案,他们分别是: Pthreads NSThread GCD NSOperation & NSOperationQueue 所以接下来,我会一一讲解这些 ...
- COGS746. [网络流24题] 骑士共存
骑士共存问题«问题描述:在一个n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘 上某些方格设置了障碍,骑士不得进入. «编程任务:对于给定的n*n个方格的国际象棋棋盘和障碍标志 ...
- 【译文】 GC 安全点 和安全区域
原文链接 : here 根引用 Root references 一个实例死了,意味着它变得无用.只用程序员知道一个实例是否已经无用.为了让程序知道一个实例是否已经无用,我们可以使用编译器分析,引用 ...
- ASP.NET 系统对象 Request(一)
Request对象 用来获取客户端在请求一个页面或传送一个Form是提供的所有信息.它包括用户的HTTP变量.能够识别的浏览器.存储客户端的Cookie信息和请求地址等. Request对象是Syst ...
- 检查PHP文件中是否含有bom的PHP函数
<?php /*检测并清除BOM*/ if(isset($_GET['dir'])){ $basedir=$_GET['dir']; }else{ $basedir = '.'; } $auto ...
- Linux文件查找命令 find 详解
关于find命令 由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下.即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只你具有相应的权 ...
- Mac Pro 编译安装 Redis-3.2.3
Redis官方下载地址:http://redis.io/download Redis安装 cd /usr/local/src/redis-3.2.3 sudo make sudo make insta ...
- 【Alpha版本】冲刺-Day10
队伍:606notconnected 会议时间:11月18日 会议总结 张斯巍(433) 今天安排:完成剩余界面的设计,小修小补 完成度:100% 遇到的问题:无 感想:因为自己是负责界面美工部分的, ...
- 帝国CMS视频
http://list.youku.com/albumlist/show?id=17602333&ascending=1.html