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. org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.AccessControlException)

    在运行hadoop的程序时,向hdfs中写文件时候,抛出异常信息如下: Caused by: org.apache.hadoop.ipc.RemoteException(org.apache.hado ...

  2. 常用hive的CLI命令

    1.show tables  --查看所有表 2.desc tabname --查看表信息 3.dfs -ls 目录  查看hdfs上面的文件  dfs -lsr /user  递归显示目录/user ...

  3. MongoDB启动报错

    启动mongodb的时候报错: [root@localhost bin]# ./mongod --dbpath /usr/java/mongoNode/data/db --logpath /usr/j ...

  4. VS2015编译GDAL2.2.1源码

    下载完GDAL2.2.1,你会发现这货没有CMakeLists.txt,对于我这样的只会用CMake GUI的货来说,着实很难过. 需要用VS带的nmake来生成.sln 管理员身份启动VS2015命 ...

  5. 如何使用微信小程序制作banner轮播图?

    在前端工程师的工作中,banner是必不可少的,那缺少了DOM的小程序是如何实现banner图的呢?如同其他的框架封装了不同的banner图的方法,小程序也封装了banner的方法,来让我一一道来: ...

  6. jQUery中closest和parents的主要区别是

    ①,前者从当前元素开始匹配寻找,后者从父元素开始匹配寻找: ②,前者逐级向上查找,直到发现匹配的元素后就停止了,后者一直向上查找直到根元素,然后把这些元素放进一个临时集合中,再用给定的选择器表达式去过 ...

  7. Mybaits中的update

    <update id="update" parameterType="Currency"> UPDATE YZ_SECURITIES_CURRENC ...

  8. ajax的历史

    ajax (AJAX开发) 编辑 AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX ...

  9. Tomcat7.0无法启动解决方法[failed to start]

    很奇怪的一个问题,Tomcat一直好好的,运行Servlet之后就报这个错: 为什么呢?在网上查都查不到解决方法,后来仔细检查了下Servlet,发现web.xml有个低级错误: 配置的Servlet ...

  10. 2015.10.11(js判断鼠标进入容器的方向)

    判断鼠标进入容器的方向 1.前几天在万圣节专题项目中用到了鼠标坐标page事件,随着鼠标背景图片移动形成有层次感的效果,但page事件在IE低版本不支持,所以还要做兼容.在研究page事件同时无意中想 ...