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.
这道题,会看到重复的数字,去重(判断相等的话,就移位,这是给我自己写的)的办法可以很好解决。
因为循环移位的数组,mid在中间划分,会变为两个不对称的子数组:
其中一个有序,另一个无序。只要抓住这一条,就可以很好利用二分搜索去掉不想要的那一部分了。
这一体后面会对临界值处理,可以另外加判断条件。
if(nums.size()==)return ;
int left = ;
int right = nums.size()-;
int mark = right;
while(left<=right){
if(left+==right){
mark = nums[left]<nums[mark]?left:mark;
mark = nums[right]<nums[mark]?right:mark;
break;
}
int mid = (left+right)/;
if(nums[mid]<nums[right]){
if(nums[mid]<nums[mark])
mark = mid;
right = mid-;
}else if(nums[mid]>nums[right]){
if(nums[left]<nums[mark])
mark = mid;
left = mid+;
}else{
right--;
}
}///while if(left<nums.size())
mark = nums[left]<nums[mark]?left:mark;
if(right>)
mark = nums[right]<nums[mark]?right:mark;
return nums[mark];
154 Find Minimum in Rotated Sorted Array II的更多相关文章
- 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 ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)
[LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【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】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 ...
- 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 ...
- [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. ...
- [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 ...
- 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 ...
- [LeetCode#154]Find Minimum in Rotated Sorted Array II
The question: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are ...
随机推荐
- OpenCV图像Surf与flann特征点(转载)
Surf(Speed Up Robust Feature) Surf算法的原理 ...
- xmind的第三天笔记
- CrossApp 0.3.1示例编译问题解决过程
1 AlertTest.h找不到 问题成因:HelloCpp工程中头文件搜索路径没有增加Classes目录,需要自己加进去.(另外由于这些文件都是在子目录中,用递归模式也行,逐个子目录添加也行) 2 ...
- Android 中类似ModelWindow的一个实现
Android里一般的画面(Activity)都是尽量占满整个屏幕,这样符合单线程的设计, 而有些类似popup之类的子画面,却希望他弹出来的时候表现的如同web的模态窗口 (ModelWindow, ...
- mysql修改root密码和设置权限
整理了以下四种在MySQL中修改root密码的方法,可能对大家有所帮助! 方法1: 用SET PASSWORD命令 mysql -u root mysql> SET PASSWORD FOR ' ...
- UVa 489 HangmanJudge --- 水题
UVa 489 题目大意:计算机给定一个单词让你猜,你猜一个字母,若单词中存在你猜测的字母,则会显示出来,否则算出错, 你最多只能出错7次(第6次错还能继续猜,第7次错就算你失败),另注意猜一个已经猜 ...
- kuangbin_UnionFind J (POJ 2492)
加权并查集mod2模板 本体的难点是bug的释义(笑) #include <iostream> #include <string> #include <cstdio> ...
- MFC中使用Duilib--1
网上找到Duilib入门教程中,第一个给的时基于SDK的例子,在这里,自己写了个MFC的,与入门教程中的例子一样. 新建一个窗口类(CTestDlg) TestDlg.h内容如下: [cpp] vie ...
- HashMap和HashSet的区别
理解HashSet及使用 HashMap和HashSet的区别是Java面试中最常被问到的问题.如果没有涉及到Collection框架以及多线程的面试,可以说是不完整.而Collection框架的问题 ...
- MUA
a big deal analysis analytics cooperate 合作 efficient explicitly fine grained Granularity graph geogr ...