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 ...
随机推荐
- 215. Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- launch文件概述---1
摘要: 原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 资源链接:http://wenku.baidu.com/link?url=PhyN3C6ghqo ...
- leetcode 112 Path Sum ----- java
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- leetcode 93 Restore IP Addresses ----- java
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- 去除包裹的a标签
<div id="test"> <a href="http://www.cnblogs.com">Link 1</a> ...
- 无shell情况下的mysql远程mof提权利用方法详解
扫到一个站的注入<ignore_js_op> 在havij中得到mysql数据库中mysql库保存的数据库密码:<ignore_js_op> 有时候发现1.15版的还是最好用, ...
- dump java
http://www.gamlor.info/wordpress/2011/09/visualvm/ https://visualvm.java.net/zh_CN/gettingstarted.ht ...
- android屏幕亮度
/** * 获得当前屏幕亮度的模式 * SCREEN_BRIGHTNESS_MODE_AUTOMATIC=1 为自动调节屏幕亮度 * SCREEN_BRIGHTNESS_MODE_MANUAL=0 为 ...
- Integer.parseInt()和Integer.valueOf()有什么区别
jdk的源代码的时候注意到Integer.parseInt(s) 和 Integer.valueOf(s)的具体代码的实现有所区别: Java代码 public static int parseInt ...
- asterisk
http://www.asterisk.org/ asterisk is the world's most widely adopted open source commnuctions platfo ...