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 ...
随机推荐
- abap append 用法
[转自http://blog.chinaunix.net/uid-7982817-id-91999.html]Append用法总结 2008-11-14 11:42:19 分类: Syntax APP ...
- 无线路由器wds桥接技术+丢包率
半根毛线http://www.cnblogs.com/hsd-/ 今天下午鼓捣了一下无线路由的wds桥接 算是计算机网络的作业 码来分享一下 1.首先设置主路由 我的主路由是斐讯4线 路由ip为192 ...
- SQL根据现有表新建一张表
SQL根据现有表新建表,新建的这张表结构要跟现有表结构相同,但不要现有表里面的数据! 执行DML语句依据数据库类型而定: SQLITE -----复制表结构及数据到新表 CREATE TABLE TA ...
- php部分--例子:租房子(复选框的全选、数组拼接成字符串、设置复选框的name值、)
1.链接数据库 <?php include("DBDA.class.php"); $db=new DBDA(); $sql="select * from fangz ...
- leetcode 101 Symmetric Tree ----- java
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- HDU-1828 Picture(扫描线)
题目大意:给若干个矩形,求轮廓边长. 题目分析:与求面积类似.按从下往上扫描,仍然是底边添加,上边删除.但要同时维护竖边的数目,每次扫描对答案的贡献为扫描线上总覆盖长度的变化量加上竖边的增量.总覆盖长 ...
- javascript this 代表的上下文,JavaScript 函数的四种调用形式
JavaScript 是一种脚本语言,支持函数式编程.闭包.基于原型的继承等高级功能.其中JavaScript 中的 this 关键字,就是一个比较容易混乱的概念,在不同的场景下,this会化身不同的 ...
- linq字符串搜索条件,排序条件-linq动态查询语句 Dynamic LINQ
在做搜索和排序的时候,往往是前台传过来的字符串做条件,参数的数量还不定,这就需要用拼sql语句一样拼linq语句.而linq语句又是强类型的,不能用字符串拼出来. 现在好了,有个开源的linq扩展方法 ...
- JSP 相关试题(四)
简答 1.有人说:servlet和 JavaBean都是java类,可以互换使用,请您回答,在MVC中,控制器部分是否可以使用JavaBean完成?为什么? 不能.因为javabean是普通的java ...