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.

SOLUTION 1:

请参考前一个题目Find Minimum in Rotated Sorted Array

1. 如何找中间断开的区间(也就是说旋转过)
我们的目的是要找出存在断口的地方。所以我们可以每次求一下mid的值,把mid
跟左边比一下,如果是正常序,就丢掉左边,反之丢掉右边,不断反复直到找到断口。
分析一下:
比如4 5 6 7 0 1 2  从中间断开后,它是由一个有序跟一个无序的序列组成的。
如果left = 0, right = 6,mid = 3, 那么4, 5, 6, 7 是正序, 7, 0, 1,
2是逆序,所以我们要丢掉左边。这样反复操作,直到数列中只有2个数字,就是断开处,这题我们会得到7,0,返回后一个就可以了。

以下图示简单描述了通过三步操作逐步逼近断口处。每一次我们可以扔掉一半,速度是LogN.

2. 特别的情况:

如果发现 A.mid > A.left,表示左边是有序,丢掉左边。

如果发现 A.mid < A.left, 表示无序的状态在左边,丢掉右边

如果A.mid = A.left,说明无法判断。这时我们可以把left++,丢弃一个即可。不必担心丢掉我们的目标值。因为A.left == A.mid,即使丢掉了left,还有mid在嘛!

每次进入循环,我们都要判断A.left < A.right,原因是,前面我们丢弃一些数字时,有可能造成余下的数组是有序的,这时应直接返回A.left! 否则的话 我们可能会丢掉解。

就像以下的例子,在1 10 10中继续判断会丢弃1 10.

举例: 10 1 10 10 如果我们丢弃了最左边的10,则1 10 10 是有序的

3.对复杂度的影响:

题目中问到了,对复杂度有何影响:实际上是有的,如果全部的数字相等,我们就退化为O(N),但是平均的复杂度仍然是O(LogN),最后复杂度的大小取决于重复的数字的多少。如果重复字数少,与logN相差不大。

 public class Solution {
public int findMin(int[] num) {
if (num == null || num.length == 0) {
return 0;
} int len = num.length;
if (len == 1) {
return num[0];
} else if (len == 2) {
return Math.min(num[0], num[1]);
} int left = 0;
int right = len - 1; while (left < right - 1) {
int mid = left + (right - left) / 2;
// In this case, the array is sorted.
// 这一句很重要,因为我们移除一些元素后,可能会使整个数组变得有序...
if (num[left] < num[right]) {
return num[left];
} // left side is sorted. CUT the left side.
if (num[mid] > num[left]) {
left = mid;
// left side is unsorted, right side is sorted. CUT the right side.
} else if (num[mid] < num[left]) {
right = mid;
} else {
left++;
}
} return Math.min(num[left], num[right]);
}
}

2015.1.1 Redo:

 public class Solution {
public int findMin(int[] num) {
if (num == null || num.length == ) {
return ;
} int l = ;
int r = num.length - ; while (l < r - ) {
int mid = l + (r - l) / ; // The array is sorted.
if (num[l] < num[r]) {
return num[l];
} if (num[mid] < num[r]) {
r = mid;
// left side is sorted. discard the left side.
} else if (num[mid] > num[l]) {
l = mid;
} else {
l++;
}
} return Math.min(num[l], num[r]);
}
}

GitHub Code:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/binarySearch/FindMin2.java

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

    Find Minimum in Rotated Sorted Array Question Solution Suppose a sorted array is rotated at some piv ...

  3. 【原创】leetCodeOj --- Find Minimum in Rotated Sorted Array II 解题报告

    题目地址: https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目内容: Suppose a sort ...

  4. 【刷题-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 ...

  5. 【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 ...

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

  7. LeetCode: Search in Rotated Sorted Array II 解题报告

    Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...

  8. 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...

  9. Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II)

    Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II) 假设按照升序排序的数组在预先未知的某个点上进 ...

随机推荐

  1. 〖Linux〗Ubuntu13.10,配置tftp服务器

    前言,配置了好久没有发现老是出问题tftp: server error: (2) Access violation,一般侦测之后... 1. 安装软件包:apt-getsudo apt-get ins ...

  2. Oracle EBS SLA(子分类账)

    SLA概述 SLA(Subledger Accounting) 子帐是子分类帐会计的简称,字面上的含义就是子分类帐会计分录 SLA常用表介绍 在SLA中技术方面最常用的就是日记账来源追溯,在追溯的过程 ...

  3. 18、Java中可变参数

    从JDK 1.5之后,Java允许定义形参可变的参数 例如: public void test(int a,String ... books){ for(String book:books){ Sys ...

  4. 批处理文件:将目录下所有的jar文件都加到CLASSPATH

    简便写法如下: 代码 : @echo off SetLocal EnableDelayedExpansion FOR %%i IN ("XXX\lib\*.jar") DO SET ...

  5. (原+转)ubuntu16中安装opencv2.4.11(2.4.13)

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5638117.html 参考网址: http://www.cnblogs.com/jeakon/arch ...

  6. delphi實現按键精靈的功能

    unit kbKernel; interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Fo ...

  7. JavaScript 设计模式之代理模式

    一.代理模式概念解读 1.代理模式概念文字解读 代理,顾名思义就是帮助别人做事,GOF对代理模式的定义如下: 代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问.代理模式使得代理对象 ...

  8. mongodb 安装教学

    安装文件:mongodb-win32-x86_64-2008plus-ssl-3.2.6-signed.msi 电脑配置:win7 64位 MongoDB的安装很简单,设置好安装路径后,一直Next直 ...

  9. go 学习笔记(1)--package

    引入包有以下几种方式: 1. 最简单的方式引入一个包的方式是直接引入包,例如: import "fmt" import "os" 2. 也可以通过下面的方式将包 ...

  10. VS报:"dll标记为系统必备组件,必须对其进行强签名"错误

    问题: VS生成程序时,报“要将程序集“XX.dll”标记为系统必备组件,必须对其进行强签名.”错误. 解决方法: 1)在报错的解决方案中找到一个可以发布的项目(引用该XX.dll的项目未必可以发布) ...