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 ...
随机推荐
- 2015苏州大学ACM-ICPC集训队选拔赛(2)1004
草爷要的数 Problem Description 今天校队队员们准备放松一下,我们队就准备选一些数字玩,然而每个人喜欢的数字是不同的,刻盘喜欢x(1<=x<=1^9),凯凯喜欢y(1&l ...
- Go语言构建json和解析json实例
参考网址如下: https://www.cnblogs.com/fengbohello/p/4665883.html
- $.extend({},defaults, options)
1.$.extend({},defaults, options) 这样做的目的是为了保护包默认参数.也就是defaults里面的参数. 做法是将一个新的空对象({})做为$.extend的第一个参数, ...
- 数据库迁移expdp impdp 与 OGg 搭建
1.long 字段的无法使用OGG 同步 2.clob字段的导入导出Bug , 生产使用network-link 导入导出太慢了,本地导入导出速度会快3到4倍 .但是测试环境的情况却相反 测试环境和生 ...
- Web 2.0 浏览器端可靠性测试第2部分(如何发现和分析 Web 2.0 浏览器端的内存泄漏)
介绍浏览器端的可靠性测试 在上一编文章中我们介绍了浏览器端可靠性测试的概念.测试方法.以及常用的测试和分析工具.我们知道,浏览器端可靠性测试,就是以浏览器为测试平台,通过模拟用户在真实场景下的页面操作 ...
- jemalloc报 Unsupported system page size错误
- 标准I/O库(详解)(Standard I/O Library)
文章转自:https://www.cnblogs.com/kingcat/archive/2012/05/09/2491847.html 自己在学习中,对此原文的基础之上进行补充. 什么是缓冲区 缓冲 ...
- SWIG 和 Python——c/c++与脚本交互
C 和 C++ 被公认为(理当如此)创建高性能代码的首选平台.对开发人员的一个常见要求是向脚本语言接口公开 C/C++ 代码,这正是 Simplified Wrapper and Interface ...
- Linux kernel rbtree
Linux kernel rbtree 因编写内核模块时需要用到rbtree来记录异步request,研究分析了一下kernel rbtree的使用方法,记录于此.本文主要参考了内核文档rbtree. ...
- java学习第十四天
1:正则表达式(理解) (1)就是符合一定规则的字符串 (2)常见规则 A:字符 x 字符 x.举例:'a'表示字符a \\ 反斜线字符. \n 新行(换行)符 ('\u000A') \r 回车符 ( ...