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

 1 class Solution:
 2     # @param num, a list of integer
 3     # @return an integer
 4     def findMin(self, num):
 5         # none case
 6         if num is None :
 7             return None
 8         # only one element case
 9         if len(num)==1 :
10             return num[0]
11         # binary search case
12         start = 0
13         end = len(num)-1
14         while start<end and num[start]>=num[end]:
15             mid = (start+end)/2
16             if num[start]>num[end]:
17                 if num[mid]>num[end] :
18                     start = mid+1
19                 else:
20                     end = mid
21             else:
22                 if num[mid]>num[end] :
23                     start = mid+1
24                 else :
25                     start = start+1
26         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]了。

时间: 2024-10-09 16:17:35

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/ 解题思路:这道题和上一道题的区别是,数组中可能有相同的数.那么,分下列几种情况: 代码: class Solution: # @param num, a list of integer # @return an integer def findMin(self, num): L = 0; R = len(num)-1 while L < R

leetcode -Find Minimum in Rotated Sorted Array II (1)

本人大三狗,大一学物理,大二转专业来了计院.一入计院深似海,从此节操是路人.转眼间一年过去了,基本上课本的知识学的很好,考前突击分数还很光鲜,但是总是觉得空虚.因为在这个讲究技术的年代,没有一点技术压身,是很容易睡不着觉的.近日阅读了不少前人的经验教训,感觉自己的目标很明确,应届入bat,有必要考个研也没问题,保研估计没戏了.在这个讲究实战的年代,我有必要积累一点代码行数了,否则坑定是混不过面试的.而且还自以为是地定制了一批书单,现在看到堆到50cm搞的一堆书,也觉得压力山大.我就是属于这种书看

LeetCode Find Minimum in Rotated Sorted Array II

Find Minimum in Rotated Sorted Array II Total Accepted: 23090 Total Submissions: 73108 My Submissions Question Solution Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexi

Leetcode#154Find Minimum in Rotated Sorted Array II

Find Minimum in Rotated Sorted Array II Total Accepted: 25678 Total Submissions: 80978My Submissions Question Solution Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity

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 might become 4 5 6 7 0 1 2). Find the minimum element. The array may contain duplicates. 解题思路: 参考Java for LeetCode 081 Search in Rotated Sorted Array II J

[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 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 be

LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法

Find Minimum in Rotated Sorted Array II 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 be

【LeetCode】Find Minimum in Rotated Sorted Array II (2 solutions)

Find Minimum in Rotated Sorted Array II 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

[leetcode]Search in Rotated Sorted Array II @ Python

原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if

leetcode_154_Find Minimum in Rotated Sorted Array II

欢迎大家阅读参考,如有错误或疑问请留言纠正,谢谢 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 might become 4 5 6 7 0 1 2). Find the minimum element. The array may contain duplica