leetcode 【 Find Minimum in Rotated Sorted Array 】python 实现

题目

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

 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         # short lenght case
 9         if len(num)==1 :
10             return num[0]
11         # binary search
12         start = 0
13         end = len(num)-1
14         while start<=end :
15             if start==end :
16                 return num[start]
17             if start+1==end :
18                 return min(num[start],num[end])
19             mid = (start+end)/2
20             if num[mid]>num[start] :
21                 if num[mid]>num[end] :
22                     start = mid
23                 else:
24                     return num[start]
25             else:
26                 if num[mid]>num[end] :
27                     return num[end]
28                 else:
29                     end = mid

思路

基本思路还是binary search。

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

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

时间: 2024-08-07 12:31:58

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

[leetcode] Find Minimum in Rotated Sorted Array @ Python

source: https://oj.leetcode.com/problems/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 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no du

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

Find Minimum in Rotated Sorted Array Total Accepted: 42341 Total Submissions: 127863My Submissions Question Solution 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

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

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 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array. 在任何一个sublist中,如果头元素大于尾元素,那么这个minimum一定在这个sublist中间

[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 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array. 这道题是要求找出一个数组中的最小值,但是这个数组是在有序数组的基础上循环右移了K次. 提示可以用二分

[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 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array. Hide Tags Array Binary Search 二分查找的变形,比较神奇的是数列可能没有

[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 旋转序列找最小值(二分查找)

题意:有一个有序序列A,其内部可能有部分被旋转了,比如A[1...n]被转成A[mid...n]+A[1...mid-1],如果被旋转,只有这种形式.问最小元素是?(假设没有重复元素) 思路:如果是序没乱,直接返回A[1],如果乱了,二分查找还是可以的,O(1)可能就不行了. 二分要点:mid有可能就是所要找的最小元素,所以不能轻易写出l=mid+1这样的语句,可能最小值就被忽略过了,因为我们无法直接判断A[mid]是否就是最小值.所以尽量应该是l=mid这样写,但是要防止死循环. 具体来说,可