154. Find Minimum in Rotated Sorted Array II(Binary search)
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/ -- leetcode
follow up question: find the minimum element in a rotated array with duplicate elements
Idea: [3,3,1,3] compared to [3,4,1,2] -> l = mid+1 --1
[1,3,3] compared to [1,3,4] - > r = mid; -- 2
If we used the previous solution, it will complain with the above cases because one case is : nums[mid] > nums[r] l = mid+1; violating the 2nd case to update r ot l
So here is the hadful problem.
At first, I am trying to seperate this case with one conditon: nums[mid] == nums[r] or..... However that is not general
Totally, (nums[l] == nums[mid]) && (nums[r] == nums[mid]) {left++, right--} skip相同的元素。知道不同为止。
Dealing with special case [1,1] or [1] -- determine the case of left and right.
Idea is from the https://leetcode.com/problems/search-in-rotated-sorted-array-ii/description/ -- search in rotated array.
public int findMin(int[] nums) {
int l =0, r = nums.length-1;
while(l<r){
int mid = l+(r-l)/2;
while((nums[l] == nums[mid]) && (nums[r] == nums[mid]) ) {
++l; --r;
if(l>=nums.length || r<0) break;
}
if(l==r) return nums[l];
else if(l>r) return nums[r+1];
if(nums[mid] > nums[r]) l = mid+1;
else r = mid;
}
return nums[l];
}
follow up: find the minimum value index
Other thoughts for this problem: if(nums[mid] == nums[r]) h--; /./skip the current high one..
好难得二分查找。。。。
to be continued...
154. Find Minimum in Rotated Sorted Array II(Binary search)的更多相关文章
- 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 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- 【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 ...
随机推荐
- 杭电ACM hdu 2079 选课时间 (模板)
Problem Description 又到了选课的时间了,xhd看着选课表发呆,为了想让下一学期好过点,他想知道学n个学分共有多少组合.你来帮帮他吧.(xhd认为一样学分的课没区别) Input输入 ...
- JDK Throwable
Throwable 1. 使用大量数组和List常量: private static final StackTraceElement[] UNASSIGNED_STACK = new StackTra ...
- logback+spring实践
配置文件名称使用: logback-spring.xml 配置user.home是jvm传过来的系统参数,可以直接使用 <property name="LOG_PATH&quo ...
- VMware硬盘空间——扩容
VMware原来分配的硬盘空间太小了--扩容 找到VMware的安装路径,如我是默认安装的:C:\Program Files (x86)\VMware\VMware Workstation,打开该路径 ...
- The 'gridview' module MUST be setup in your Yii configuration file.
解决方法:common的config的main.php中添加 'gridview' => ['class' => 'kartik\grid\Module'], 在vender的compos ...
- java——newInstance()方法和new关键字
https://www.cnblogs.com/liuyanmin/p/5146557.html 这两个都可以创建一个对象,那么这样个东西有什么不一样呢?什么时候用new,什么时候用newInstan ...
- APP测试总结1
1.安装.卸载测试 安装.卸载测试主要针对编译后源程序生成的APK安装文件 主要测试点: 1).生成的APK文件在真机上可以安装及下载 2).Android手机端的通用安装工具,如:豌豆荚及91助手等 ...
- java生成复杂word文档
在Web应用中,有时需要按照固定的模板将数据导出到Word,如流程审批单,在流程处理完成后将处理过程按照流程单的要求导出,有时程序中需要实现生成 标准Word文档,要求能够打印,并且保持页面样式不变, ...
- [OpenStack] [Liberty] Neutron单网卡桥接模式访问外网
环境配置: * Exsi一台 * Exsi创建的单网卡虚拟机一台 * Ubuntu 14LTS 64位操作系统 * OpenStack Liberty版本 * 使用Neutron网络而非Nova网络 ...
- The function getUserId must be used with a prefix when a default namespace is not specified 解决办法
The function getUserId must be used with a prefix when a default namespace is not specified 解决方法: 1. ...