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 an array sorted in ascending order 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.

 class Solution {
public int findMin(int[] a) {
if(a.length==0) return 0 ;
int left = 0,right = a.length-1,mid=0;
while(a[left]>=a[right]){
if(right-left==1) return a[right]; mid = left +(right-left)/2;
// 特殊处理[1 1 1 0 1]
// code
if((a[mid]==a[right])&&(a[mid]==a[left]))
return find_min(a,left,right);
//
if(a[left]<=a[mid]) left =mid;
if(a[right]>=a[mid])right = mid;
}
return a[mid];
}
private int find_min(int[] a,int left,int right){
int min = a[left];
for(int i =left;i<=right;i++)
if(a[i]<min)
min =a[i];
return min;
}
}

154. Find Minimum in Rotated Sorted Array II(剑指offer)的更多相关文章

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

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

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

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

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

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

  9. 154 Find Minimum in Rotated Sorted Array II

    多写限制条件可以加快调试速度. ======= Follow up for "Find Minimum in Rotated Sorted Array":What if dupli ...

随机推荐

  1. swift--获取window

    有时候,我们需要频繁的调用界面,然后给当前页面加一个跟视图,这个时候就需要找windown, 代码如下: let rootVC = UIApplication.shared.delegate as! ...

  2. 浅谈 SSD,eMMC,UFS(转自知乎)

    但作为一个计算机体系结构的研究生,在这些名词满天飞的时候,我的好奇心是抑制不住的,想一探这几样技术的究竟.本文不对某一特定事件进行点评,仅从技术角度分析对比一下这三种技术.就算是当做自己的技术储备+科 ...

  3. React的setState如何实现同步处理数据

    React里面的使用setState来进行状态的更新,为了性能的提升,此时的过程是异步操作的,那我们如果在一个进程里面想同步操作改变了状态的值怎么办呢,这里需要使用回调函数了: this.setSta ...

  4. ReactNative For Android 项目实战总结

    版权声明:本文由王少鸣原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/159 来源:腾云阁 https://www.qclo ...

  5. Android 查看每个应用的最大可用内存

    http://blog.csdn.net/vshuang/article/details/39647167    Android 内存管理 &Memory Leak & OOM 分析 ...

  6. Request.getRequestURL

    getRequestURI()就相当于你在写一个JSP页面的时候会有这样的东西"action='/WebRoot/xxx'"这个方法就是获得'/WebRoot/xxx',也就是说它 ...

  7. 代码片段,使用TIKA来解析PDF,WORD和EMAIL

    /** * com.jiaoyiping.pdstest.TestTika.java * Copyright (c) 2009 Hewlett-Packard Development Company, ...

  8. 几何+点与线段的位置关系+二分(POJ2318)

    TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10666   Accepted: 5128 Description ...

  9. SpringMVC中 解决@ResponseBody注解返回中文乱码

    问题:在前端通过get请求服务端返回String类型的服务时,会出现中文乱码问题 原因:由于spring默认对String类型的返回的编码采用的是 StringHttpMessageConverter ...

  10. jquery插件方式实现table查询功能

    1.写插件部分,如下: ;(function($){ $.fn.plugin = function(options){ var defaults = { //各种属性,各种参数 } var optio ...