leetcode-Find Minimum in Rotated Sorted Array-153

一个递增数组,没有重复的元素,可能被右移过,找出最小的元素

这和这题差不多http://www.cnblogs.com/0summer/p/5825282.html

依旧是改进过的二分:

1.如果nums[l]<nums[mid],说明[l,mid]是递增的,但是[mid,r]不确定

  1)如果nums[mid]>nums[r]说明[mid,r]包括了递增数组的首尾,那么最小值一定在这个区间,则l=mid+1;

  2)否则最小值在[l,mid]区间,则r=mid-1

2.如果nums[l]>nums[mid],说明[l,mid]包括了递增数组的首尾,那么最小值一定在这个区间,则r=mid-1

3.nums[l]=nums[mid]说明此时l==mid,则l++

 1 class Solution {
 2 public:
 3     int findMin(vector<int>& nums) {
 4         if(nums.size()==0) return 0;
 5         int l=0,r=nums.size()-1;
 6         int key=INT_MAX;
 7         //int cnt=1;
 8         while(l<=r){
 9             //cout<<cnt++<<endl;
10             int mid=(l+r)>>1;
11             if(nums[mid]<=key){
12                 key=nums[mid];
13             }
14             if(nums[l]<nums[mid]){
15                 if(nums[mid]>nums[r]) l=mid+1;
16                 else r=mid-1;
17             }
18             else if(nums[l]>nums[mid]){
19                 r=mid-1;
20             }
21             else l++;
22         }
23         return key;
24     }
25 };
时间: 2024-10-12 10:28:57

leetcode-Find Minimum in Rotated Sorted Array-153的相关文章

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 @ 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

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这样写,但是要防止死循环. 具体来说,可