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代码】非递归
 public class Solution {
public int findMin(int[] nums) {
int start=0,mid=0,end=nums.length-1;
while(start<end){
if(nums[start] < nums[end]) return nums[start]; mid=start+(end-start)/2;
if(nums[mid]>nums[end]) start=mid+1;
else if(nums[mid]==nums[end]) end--;
else end=mid;
}
return nums[start];
}
}
【java代码】递归
 public class Solution {
public int findMin(int[] nums) {
int len = nums.length;
if(len == 1) return nums[0]; return find(nums, 0, len-1);
} public int find(int nums[], int left, int right){
while(left < right){
if(nums[left] < nums[right]) return nums[left];
if(left == right - 1) return Math.min(nums[left], nums[right]); int mid = left + (right - left)/2;
if(nums[left] == nums[mid] && nums[mid] == nums[right])
return Math.min(find(nums, left, mid-1), find(nums, mid+1, right)); if(nums[left] <= nums[mid]) left = mid + 1;
else right = mid;
}
return nums[left];
}
}

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

  1. 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)

    [LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  2. 【LeetCode】154. Find Minimum in Rotated Sorted Array II (3 solutions)

    Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...

  3. 【刷题-LeetCode】154 Find Minimum in Rotated Sorted Array II

    Find Minimum in Rotated Sorted Array II Suppose an array sorted in ascending order is rotated at som ...

  4. leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search

    这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...

  5. LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法

    Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...

  6. [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

      Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i. ...

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

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

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

随机推荐

  1. hdu2159二维费用背包

    题目连接 背包九讲----二维费用背包 问题 二维费用的背包问题是指:对于每件物品,具有两种不同的费用:选择这件物品必须同时付出这两种代价:对于每种代价都有一个可付出的最大值(背包容量).问怎样选择物 ...

  2. Defraggler(磁盘整理软件) V2.21.993 绿色版

    软件名称: Defraggler(磁盘整理软件) 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 5.0MB 图片预览: 软件简介: Defraggler ...

  3. openstack私有云布署实践【17 配置文件部份说明】

    Nova部份 cpu_allocation_ratio = 4.0 物理 CPU 超售比例,默认是 16 倍,超线程也算作一个物理 CPU,需要根据具体负载和物理 CPU 能力进行综合判断后确定具体的 ...

  4. WinForm笔记一:文本框只允许输入数字

    在WinForm的文本框中,有时候只允许数字,而不能输入除数字以外的其他字符,要调用TextBox的KeyPress事件,代码如下: //只允许输入数字 if (e.KeyChar<'0'||e ...

  5. 简易js网页实时时钟日历

    <script language="javascript"> function timeF(i){ if(i<10){ i="0"+i; } ...

  6. put a favicon for github pages

    put the picture "favicon.ico" in the root of your web page repo.then add the following lin ...

  7. Python 修炼3

    # 列表 功能方法 *补充(zip zip(list1,list2) 会形成一个[(),()]新的列表list1和list2一一对应得组成一个新的元素以元组最为单位) # 1.修改# li = [1, ...

  8. 通过mvn archetype:generate创建Maven项目模板慢的问题

    通过mvn archetype:generate这种交互方式来创建Maven项目模板的时候,经常会长时间卡在Generating project in Interactive mode这一行提示(图1 ...

  9. dump报文转换为wrieshark报文

    我们开发中经常会出原始的报文,如下所示: 45 00 01 3d 8e 6a 00 00 80 11 ab 46 00 00 00 00 ff ff ff ff 00 44 00 43 01 29 6 ...

  10. 【原创】对Java的synchronized关键字的学习

    在Java中,每一个线程都有一个内部锁.当我们使用synchronized关键字时,就是利用这个内部锁来实现线程对某个对象的锁定控制. 那么,如果某个对象中有两个方法,方法一和方法二都使用了synch ...