题目

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.

代码:oj测试通过 Runtime: 52 ms

 class Solution:
# @param num, a list of integer
# @return an integer
def findMin(self, num):
# none case
if num is None:
return None
# short lenght case
if len(num)==1 :
return num[0]
# binary search
start = 0
end = len(num)-1
while start<=end :
if start==end :
return num[start]
if start+1==end :
return min(num[start],num[end])
mid = (start+end)/2
if num[mid]>num[start] :
if num[mid]>num[end] :
start = mid
else:
return num[start]
else:
if num[mid]>num[end] :
return num[end]
else:
end = mid

思路

基本思路还是binary search。

注意修改start或end时的条件:因为mid也有可能是最小值,所以start=mid end=mid,这个跟传统二分查找时start=mid+1以及end=mid-1有所不同。

想明白这些后,代码一次AC。

【续】

自己的上一版代码还是太复杂的;复杂的原因是把binary search的常规套路生搬硬套,没有考虑这道题目要求的是最小值就可以了。

改进后的代码如下:

 class Solution:
# @param num, a list of integer
# @return an integer
def findMin(self, num):
# none case
if num is None:
return None
# short length case
if len(num)==1 :
return num[0]
# binary search
start = 0
end = len(num)-1
while start<=end and num[start]>num[end]:
mid = (start+end)/2
if num[mid]>num[end]:
start = mid+1
else:
end = mid
return num[start]

oj测试通过 Runtime: 57 ms

还是向别人的代码学习来的。

1. 如果是没有rotate的有序数组,那直接就返回最左边的元素就可以了

2. 如果有rotate的情况,情况稍微有些复杂。但是核心点只有一个:rotate前最左边的元素一定是最小的,当然也小于最右边的元素;但是rotate后,最左边的元素一定不是最小的了,而且最左边的元素一定大于最右边的元素。

搞清楚上面这个思路,问题就变得简单很多(很多if else之类的判断条件就可以省略了)

3. 之前自己写binary search总是爱把start==end(start跟end重叠了) start+1==end(只剩两个元素了)两种case单独拎出来,然后放心大胆地用mid=(start+end)/2再进行后面的判断。这种方法的好处是可以放心大胆,缺点就是代码有时候比较冗长。跟别人的代码学习后,可以用一个条件判断直接省略这两种special cases。

  3.1 num[start]>start[end]就可以保证不出现start==end的情况;且一旦num[start]>num[end]了,就证明当前处理的数组已经逃离rotate的影响了(见2中的红字和蓝字部分),就可以直接返回num[start]了。

  3.2 再考虑start+1==end的case: [2,1]这种情况,mid取start,则num[mid]>num[end],start=mid+1=end,退出while循环,num[start]取到了最小值;如果是[1,2],end=mid=start,同样num[start]取到了最小值。

考虑清楚上述的思路,新一版的简洁代码也就可以敲定了。

leetcode 【 Find Minimum in Rotated Sorted Array 】python 实现的更多相关文章

  1. [leetcode]Find Minimum in Rotated Sorted Array @ Python

    原题地址:https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/ 解题思路:话说leetcode上面的二分查找题目 ...

  2. Leetcode Find Minimum in Rotated Sorted Array 题解

    Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...

  3. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  4. [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

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

  5. LeetCode Find Minimum in Rotated Sorted Array II

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...

  6. LeetCode Find Minimum in Rotated Sorted Array

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...

  7. 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 migh ...

  8. LeetCode——Find Minimum in Rotated Sorted Array II

    Question Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allo ...

  9. Leetcode Find Minimum in Rotated Sorted Array I and II

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

  10. LeetCode——Find Minimum in Rotated Sorted Array

    Description: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 ...

随机推荐

  1. Activiti20180624

    1.工作流介绍 工作流(WorkFlow),是对工作流程及其各操作步骤之间业务规则的抽象.概括.描述.工作建模,即将工作流程中的工作如何前后组织在一起的逻辑和规则,在计算机中以恰当的模型进行表示并对其 ...

  2. javascript设计模式之外观模式

    /* * 外观模式 * 外观模式的主要意义在于简化类的接口,使其易于调用 */ // 你常常在不经意中使用了外观模式,尤其类库中更多(处理兼容性问题) var addEvent = function ...

  3. 微信小程序开发入门首选

    推荐一本书吧,直接上图,微信开发,微信网页开发,微信小程序开发,都用得着. 推荐一本书吧,直接上图,微信开发,微信网页开发,微信小程序开发,都用得着. 推荐一本书吧,直接上图,微信开发,微信网页开发, ...

  4. intelij idea相关笔记--持续更新

    一.快捷键: Ctrl+F 文件内查找 Ctrl+Shift+F 全局查找 Ctrl+Shift+N 查找文件 Ctrl+Alt+← 返回上一步 Ctrl+Alt+→ 返回下一步 二.编译相关: 如果 ...

  5. 美国L1签证面谈的时候一般VO会问到什么问题?

    L签证:L签证签发给被其中国公司调派到美国分公司或合资公司工作的人员.申请人必须将在美国担任经理级职务或具有专业知识,且在申请签证前的三年中至少为同一雇主或公司连续工作至少一年.签证签发费将因签证的入 ...

  6. 常用宏OC

    #ifndef MacroDefinition_h #define MacroDefinition_h //-------------------获取设备大小--------------------- ...

  7. 2018.6.4 Oracle数据库预定义的异常列表

    declare v_ename emp.ename%type; begin select ename into v_ename from emp where empno=&gno; dbms_ ...

  8. git出现误修改如何撤销

    场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file. 场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步, ...

  9. 认识mysql(2)

    1.表字段的操作 1.语法 :alter table 表名 执行动作; 2.添加字段(add) alter table 表名 add 字段名 数据类型; alter table 表名 add 字段名 ...

  10. http 工作模式与模块

    目录 http 工作模式与模块 http 服务器应用 MPM工作模式 prefork worker event 进程角色 httpd功能特性 http 安装 centos6配置目录 http 2.2 ...