leetcode 【 Find Minimum in Rotated Sorted Array II 】python 实现
题目:
Follow up for "Find Minimum in Rotated Sorted Array":
What if duplicates are allowed?Would this affect the run-time complexity? How and why?
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.
The array may contain duplicates.
代码:oj测试通过 Runtime: 82 ms
class Solution:
# @param num, a list of integer
# @return an integer
def findMin(self, num):
# none case
if num is None :
return None
# only one element case
if len(num)==1 :
return num[0]
# binary search case
start = 0
end = len(num)-1
while start<end and num[start]>=num[end]:
mid = (start+end)/2
if num[start]>num[end]:
if num[mid]>num[end] :
start = mid+1
else:
end = mid
else:
if num[mid]>num[end] :
start = mid+1
else :
start = start+1
return num[start]
思路:
对比之前一道题:http://www.cnblogs.com/xbf9xbf/p/4261334.html
这道题需要考虑duplicate的case。
简略地说,跟之前不带duplicate的case相比,核心的区别在:
不带duplicate的case : num[start]>num[end]是铁定成立的
带duplicate的case : num[start]>num[end]不一定是成立的,还可能是num[start]==num[end]
就是多了这么一个等号的可能,所以最直观的办法就是单独把这种等号的情况拿出来嘛。
因此需要讨论的情况一共有两种:
1. num[start]>num[end]
2. num[start]==num[end]
有人可能要问,为啥不能有num[start]<num[end]的情况?答:因为在while循环终止条件中已经限定了num[start]>=num[end];如果真有num[start]<num[end]的情况,那么当前start到end位置的数组已经是有序的了,直接return[start]就OK了。
Tips:
在AC之前还遇上几个问题:
1. 如果出现无法判断最小值是在左半边还是右半边的时候,不要同时start=start+1 end=end-1,start=start+1即可,否则会指针溢出
2. 之前在while循环条件的时候,一直写的是start<=end。就是多了这么一个等号,submit了几次一直不能AC。究其原因,如果start==end,就会导致执行start=start+1语句,不是溢出就是错。如果修改一下while循环的条件,start<end,那么当start==end的时候自动就退出循环了,并且返回num[start]了。
leetcode 【 Find Minimum in Rotated Sorted Array II 】python 实现的更多相关文章
- [leetcode]Find Minimum in Rotated Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 解题思路:这道题和上一道题的区别是,数组中 ...
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- LeetCode Find Minimum in Rotated Sorted Array II
原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...
- LeetCode——Find Minimum in Rotated Sorted Array II
Question Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allo ...
- Java for LeetCode 154 Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II)
Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II) 假设按照升序排序的数组在预先未知的某个点上进 ...
- [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值 II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)
[LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search
这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II (3 solutions)
Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...
随机推荐
- C# 发Domino邮件 报错误 Password or other security violation for database 的解决方案
错误提示: Password or other security violation for database ******* 问题产生的描述: 之前C#发邮件是好的 加上了附件部分代码之后,出现了这 ...
- npm install appium
先安装node.js npm config set registry http://registry.npm.taobao.org/ // 设置淘宝镜像 npm install chromedri ...
- 跨平台移动开发phonegap/cordova 3.3全系列教程-简介
一. 跨平台實現架構: phonegap +asp.net+jquery mobile/jqmobi 二. PhoneGap简介 PhoneGap是一个开源的开发框架,用来构建跨平台的使用HT ...
- Garmin APP开发之入门
Garmin开发-入门 先附上几个已经开发完成的app日历 up down 翻月 start 回到当前月(就差农历了) 秒表和定时器一体app界面比较简单,但是实用,长按菜单键可以切换秒表和定时器,有 ...
- ansible基本操作
ansible优点:redhat自带工具,可通过rpm或yum直接安装:客户端免安装:操作通过ssh验证操作:可以通过自定义hosts文件对可操作主机进行分类,方便批量操作 #ansible操作格式, ...
- Mybatis-Spring整合Spring
因为 MyBatis 用 SqlSessionFactory 来创建 SqlSession ,SqlSessionFactoryBuilder 创建 SqlSessionFactory ,而在 Myb ...
- Google Authenticator加强ssh安全
一.安装依赖包 软件包可以在这个地址下载:https://pan.baidu.com/s/1r0CmwbtCfNiBqU9rh_TxtA yum -y install pam-devel tar jx ...
- java Vamei快速教程20 GUI
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! GUI(Graphical User Interface)提供了图形化的界面,允许 ...
- linux 命令——53 route(转)
Linux系统的route 命令用于显示和操作IP路由表(show / manipulate the IP routing table).要实现两个不同的子网之间的通信,需 要一台连接两个网络的路由器 ...
- jQuery中常用的元素查找方法总结
$("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到的是唯一的元素 $("div&q ...