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)的更多相关文章

  1. 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 ...

  2. 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)

    [LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  3. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  4. 【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 ...

  5. 【刷题-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 ...

  6. 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 ...

  7. [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. ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. 10-排序6 Sort with Swap(0, i) (25 分)

    Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...

  2. Java String 字符串操作小结

    // 转载加编辑 -- 21 Apr 2014 1. Java字符串中子串的查找 Java中字符串中子串的查找共有四种方法,如下: 1.int indexOf(String str) :返回第一次出现 ...

  3. Go语言关键字之1--range

    https://blog.csdn.net/iamlihongwei/article/details/78842857 https://studygolang.com/articles/1952 ht ...

  4. 阿里Java开发规约(2)

    本文是对阿里插件中规约的详细解释二,关于插件使用,请参考这里 及时清理不再使用的代码段或配置信息. 说明:对于垃圾代码或过时配置,坚决清理干净,避免程序过度臃肿,代码冗余 Positive examp ...

  5. python练习六十四:EXCEL文件处理

    假设要读取number.txt文件中内容,code.txt文件内容如下 [ [1,2,3], [4,5,6], [7,8,9] ] 数据写入表格,如图 写文件(如果有文件,那直接调用就行,我这里自己先 ...

  6. Ubuntu14.04配置python接口,测试的小问题

    当遇到“ImportError:No module named google.protobuf.internal”(import enum_type_wrapper)的问题时候 solution: P ...

  7. matlab的一些关于块分类的函数~~~

    1. nlfilter(General sliding-neighborhood operations) B = nlfilter(A, [m n], fun),这是一个其中A是图像[m  n]是图像 ...

  8. 问题解决Determine IP information for eth0.. no link present check cable

    网上方法都没有解决:简单粗暴编辑里还原了默认设置OK了 网上方法1 一般解决办法: 第一步: 到/etc/sysconfig/network-scripts/ifcfg-eth<n>/et ...

  9. java——arr == null || arr.length == 0

    这两者是不同的: arr == null; int[] arr = null; arr.length == 0; int[] arr =new int[0];

  10. 转 ORACLE 查看RMAN的备份信息总结

    http://www.cnblogs.com/kerrycode/p/5684768.html 关于Oracle数据库的RMAN备份,除了邮件外,是否能通过其它方式检查RMAN备份的成功与失败呢?其实 ...