[LeetCode]111. 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 become 4 5 6 7 0 1 2).

Find the minimum element.

The array may contain duplicates.

解法1:本题旋转数组最小值的扩展,这次数组中可以存在重复元素了。上题因数组中无重复元素,因此通过判断nums[left]和nums[mid]的大小可以确定[left,mid]之间是否是严格递增的,而当存在重复元素时就不能这样简单的确定了。比如数组{0,1,1,1,1}的两种旋转{1,0,1,1,1}和{1,1,1,0,1},第一次迭代时nums[left]=nums[mid]=nums[right],这样不能确定不能确定[left,mid)或者(mid,right]是不是升序,也不知道最小元素0是在[left,mid]还是[mid,right]之中。因此当left、mid、right三者所指数组元素相同时调用顺序查找算法。

class Solution {
public:
    int findMin(vector<int>& nums) {
        int n = nums.size(), left = 0, right = n - 1, mid = 0;
        if (n <= 1) return n == 1 ? nums[0] : 0;
        while (nums[left] >= nums[right]) {
            mid = (left + right) >> 1;
            if (nums[left] == nums[mid] && nums[mid] == nums[right])
                return *min_element(nums.begin() + left, nums.begin() + right + 1);
            else if (nums[mid] <= nums[right]) right = mid;
            else left = mid + 1;
        }
        return nums[left];
    }
};

解法2:当left、mid、right三者所指数组元素相同时,可以将left右移一位,跳过这个相同元素,这样并不会影响结果,因为这个元素还有另外的副本存在与剩下的数组中。注意输入数组有两种情况:要么nums[left]<nums[right],此时数组未被旋转,可以直接返回;要么nums[left]>=nums[right],这时数组被旋转过。

class Solution {
public:
    int findMin(vector<int>& nums) {
        int n = nums.size(), left = 0, right = n - 1, mid = 0;
        if (n == 0) return 0;
        while (left < right) {
            if(nums[left] < nums[right]) return nums[left];
            mid = (left + right) >> 1;
            if (nums[left] < nums[mid])
                left = mid + 1;
            else if (nums[left] > nums[mid])
                right = mid;
            else ++left;
        }
        return nums[right];
    }
};
时间: 2024-12-27 21:06:30

[LeetCode]111. Find Minimum in Rotated Sorted Array II旋转数组最小值II的相关文章

【LeetCode】Find Minimum in Rotated Sorted Array 在旋转数组中找最小数

Add Date 2014-10-15 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

【LeetCode】Find Minimum in Rotated Sorted Array 找到旋转后有序数组中的最小值

本文原作者:大便一箩筐 文章原地址:http://www.cnblogs.com/dbylk/p/4032570.html 原题: 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 exist

[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题解:Search 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). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

Leetcode[154]-Find Minimum in Rotated Sorted Array II

Link: https://leetcode.com/problems/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

【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现

一.题目描述 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 m

&lt;LeetCode OJ&gt; Find Minimum in Rotated Sorted Array【153】

153. Find Minimum in Rotated Sorted Array My Submissions Question Total Accepted: 73048 Total Submissions: 209952 Difficulty: Medium 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

leetcode 154. Find Minimum in Rotated Sorted Array II --------- java

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