码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode:Find Minimum in Rotated Sorted Array II

时间:2016-06-12 02:29:39      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

Find Minimum in Rotated Sorted Array II




Total Accepted: 52722 Total Submissions: 152155 Difficulty: Hard

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.

Subscribe to see which companies asked this question

Hide Tags
 Array Binary Search

























思路:

承接上题【Find Minimum in Rotated Sorted Array】,当有重复元素时,退化成顺序寻找。


c++ code:

class Solution {
public:
    int findMin(vector<int>& nums) {
        
        int lo = 0,hi = nums.size()-1;
        
        while(lo < hi) {
            if(nums[lo] < nums[hi]) return nums[lo];
            int mid = lo + (hi-lo)/2;
            if(nums[lo] == nums[mid] && nums[mid] == nums[hi]) {
                lo++;
                hi--;
            } else if(nums[lo] <= nums[mid])
                lo = mid + 1;
            else
                hi = mid;
        }
        return nums[lo];
    }
};


LeetCode:Find Minimum in Rotated Sorted Array II

标签:

原文地址:http://blog.csdn.net/itismelzp/article/details/51622920

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!