154. Find Minimum in Rotated Sorted Array II (Array; Divide-and-Conquer)

Suppose an array sorted in ascending order 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.

思路:有重复元素的时候,不能按I中这么分三类(反例:Input: [3,3,1,3]=>将roate in right判断成没有rotate)。解决方法是,当碰到nums[start]=nums[end]的情况时,end-1,寻找不同元素再进行二分法。

class Solution {
public:
    int findMin(vector<int>& nums) {
        return dfs(nums,0,nums.size()-1);
    }

    int dfs(vector<int>& nums, int start, int end){
        if(start==end) return nums[start];
        if(nums[start]==nums[end]) return dfs(nums,start, end-1);

        int mid = start + ((end - start) >> 1);
        if(nums[mid] > nums[end]){ //rotate in the right
            return dfs(nums, mid+1,end);
        }
        else if(nums[start] <= nums[mid]){ //no rotate
            return nums[start];
        }
        else{ //rorate in the left
            return dfs(nums, start, mid);
        }
    }
};
时间: 2024-09-29 05:26:17

154. Find Minimum in Rotated Sorted Array II (Array; Divide-and-Conquer)的相关文章

LeetCode 153, 154. Find Minimum in Rotated Sorted Array I &amp; II

153. Find Minimum in Rotated Sorted Array 二分题目,由于rotated存在,a[mid]<key不能判断在哪一边搜索. 可以根据a[low]与a[high]的关系,来判断哪一边有序,哪一边存在rotate,进而缩小搜索区间. 开区间写法:(由于搜索区间和解区间都是[low, high],直接 [low, high]) class Solution { public: int findMin(vector<int>& nums) { int

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

[leedcode 154] 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 beforehand. (i.e., 0 1 2 4 5 6 7 might be

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 OJ 154. 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 beforehand. (i.e., 0 1 2 4 5 6 7 might be

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

154. Find Minimum in Rotated Sorted Array II(Binary search)

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/ -- leetcode follow up question: find the minimum element in a rotated array with duplicate elements Idea: [3,3,1,3] compared to [3,4,1,2]  -> l = mid+1 --1 [1,3,3] com

154. Find Minimum in Rotated Sorted Array II

就比1多了一点点 1 public int findMin(int[] nums) { 2 if(nums.length == 0) { 3 return -1; 4 } 5 int left = 0; 6 int right = nums.length - 1; 7 while(left < right) { 8 int mid = left + (right - left) / 2; 9 if(nums[mid] > nums[right]) { 10 left = mid + 1; 11