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 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 解题报告-二分法模板解法的更多相关文章
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)
[LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- LeetCode 新题: Find Minimum in Rotated Sorted Array 解题报告-二分法模板解法
Find Minimum in Rotated Sorted Array Question Solution Suppose a sorted array is rotated at some piv ...
- 【原创】leetCodeOj --- Find Minimum in Rotated Sorted Array II 解题报告
题目地址: https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目内容: Suppose a sort ...
- 【刷题-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 ...
- 【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 ...
- 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 ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...
- Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II)
Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II) 假设按照升序排序的数组在预先未知的某个点上进 ...
随机推荐
- SettingsEditplus
迁移时间:2017年5月20日10:51:51Author:Marydon官网下载 http://www.editplus.com/设置一 1.修改字体大小及背景色 首选项-->Genera ...
- 微信小程序-自定义底部导航
代码地址如下:http://www.demodashi.com/demo/14258.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...
- Toast连续弹出的问题
public class CommUtils { private static Toast toast = null; public static void showToast(int text) { ...
- opencv-3.0.0-beta和opencv2版本号的差别
我的机器:64位系统 第一步: opencv官网下载opencv3.0.0-beta版本号.解压到自己的目录,我的目录是E:\,解压后在E盘出现名为opencv的目录.该目录下有两个子目录 第二步:配 ...
- Spring Data JPA实体详解
1. Spring Data JPA实体概述 JPA提供了一种简单高效的方式来管理Java对象(POJO)到关系数据库的映射,此类Java对象称为JPA实体或简称实体.实体通常与底层数据库中的单个关系 ...
- Python知识总结(二)
一.import和reload和__import__ import是一个关键字,只引入一次 reload是一个函数,参数为一个字符串,它总是会重新引入 __import__是一个函数,与import关 ...
- SDUT 2608 Alice and Bob (巧妙的二进制)
Alice and Bob Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Alice and Bob like playing ...
- Android音乐播放器开发
今日看书,看到这个播放器,我就写了个例子,感觉还行,这个播放器能播放后缀是.MP3的音乐,这个例子在main.xml设置listView的时候,注意:android:id="@+id/and ...
- TFS 切换登录用户的方法[转]
来自:http://blog.csdn.net/tiangaojie123abc/article/details/12121929 方法一 用VS2010开发项目,一直困扰着我的是不知道怎么去切换TF ...
- Python学习笔记010——作用域
1 作用域的分类 全局变量:在文件中所有函数外部创建的变量,整个文件可见 局部变量:在函数.类等内部创建的变量且仅用在函数内部的变量: 函数的形参也是局部变量. 注:所有的变量必须是先创建,再使用. ...