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

Find the minimum element.

The array may contain duplicates.



【题目分析】

与Find Minimum in Rotated Sorted Array题目相比,这个题目中数组元素可能是重复的。

【思路】

在上一个题目中,我们使用二分查找来找到数组的断点,从而确定最小值的位置。在没有重复元素的时候,可以比较中间值和边界值的大小来判断中间值的位置(即位于数组的前半段还是后半段)从而确定最小值和中间值的位置关系。但是如果有重复元素的出现会导致我们无法确定中间值和最小值的位置关系,比如:[10,1,10,10]和[10,10,1,10],两个数组的中间值都是10,但是我们无法判断最小值在中间值的左边还是右边,因为这两种情况都有可能出现。但是我们可以确定的是,这种情况中间值的左边或者右边肯定有一段全为10,而最小值在另外一段。

1. 递归

如果出现上述情况,我们分别在中间值的左右两边进行查找,递归的好处是,即使需要对两段数组进行二分查找,但是整体的时间复杂度还是不错的。

2. 非递归,删除边界值

上诉情况中出现了中间值和边界值相等的情况,因此在删除边界值后的数组中进行查找并不会影响到我们的查找结果。


【java代码】非递归

 1 public class Solution {
 2     public int findMin(int[] nums) {
 3         int start=0,mid=0,end=nums.length-1;
 4         while(start<end){
 5             if(nums[start] < nums[end]) return nums[start];
 6
 7             mid=start+(end-start)/2;
 8             if(nums[mid]>nums[end]) start=mid+1;
 9             else if(nums[mid]==nums[end]) end--;
10             else end=mid;
11         }
12         return nums[start];
13     }
14 }

【java代码】递归

 1 public class Solution {
 2     public int findMin(int[] nums) {
 3         int len = nums.length;
 4         if(len == 1) return nums[0];
 5
 6         return find(nums, 0, len-1);
 7     }
 8
 9     public int find(int nums[], int left, int right){
10         while(left < right){
11             if(nums[left] < nums[right]) return nums[left];
12             if(left == right - 1) return Math.min(nums[left], nums[right]);
13
14             int mid = left + (right - left)/2;
15             if(nums[left] == nums[mid] && nums[mid] == nums[right])
16                 return Math.min(find(nums, left, mid-1), find(nums, mid+1, right));
17
18             if(nums[left] <= nums[mid]) left = mid + 1;
19             else right = mid;
20         }
21         return nums[left];
22     }
23 }
时间: 2024-11-06 07:26:44

LeetCode OJ 154. Find Minimum in Rotated Sorted Array II的相关文章

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 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法

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

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

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

[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

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]=>将

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