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 ...
随机推荐
- Experimental Educational Round: VolBIT Formulas Blitz C
Description The numbers of all offices in the new building of the Tax Office of IT City will have lu ...
- spring webapp的配置文件放置在项目外的方法
在web.xml中,填写 <context-param> <param-name>CFG_HOME</param-name> ...
- 效率工具(fswatch,rsync)
mac 安装 brew: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/in ...
- Transform控制的物体移动
Transform控制的物体移动:public class TKMove : MonoBehaviour { public float HSpeed; public float VSpeed; pri ...
- [转]jQuery: get table column/row index remove table column (by column number)
本文转自:http://www.xinotes.org/notes/note/1087/ <!DOCTYPE html><html><head> <title ...
- socket编程(一)
因为下载器涉及到socket的知识,就花了一天学习了.因为时间原因分成几部分.(这里记录上的是基于Windows平台的) #include <stdio.h> #include <w ...
- 使用cookie的注意事项
一.什么是cookie? Cookie(复数形态Cookies),中文名称为小型文本文件或小甜饼[1],指某些网站为了辨别用户身份而储存在用户本地终端(Client Side)上的数据(通常经过加密) ...
- C#语言开发规范
1. 命名规范 a) 类 [规则1-1]使用Pascal规则命名类名,即首字母要大写. eg: Class Test { ... } [规则1-2]使用能够反映类功能的名词或名词短语命名类. [规则 ...
- web子项目的路径问题
http://baidu.com/userms/ userms是一个子应用程序,项目中使用的路径 /content/css/.. 从http://baidu.com/ 开始 ~/content/ ...
- 模糊查询(like)
1. 找出名中包含 “厂”的所有供应商的名select * from provider where pro_name like '%厂%'2.第二个字为华select * from provider ...