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. 【渗透测试学习平台】 web for pentester -7.文件包含

    Example 1 输入单引号,报错,得到物理路径 可通过../../../../etc/paaswd 读取敏感信息 可包含本地文件或远程文件 https://assets.pentesterlab. ...

  2. linux文件类型详解

    *nix 有各种的文件类型 当#ls -la后,会发现在权限位前有个 - 有些是 b 有些是 d 这个位置就是文件类型的标示   普通文件regular file,  用 - (破折号)标示, 比如 ...

  3. Linux命令行常用光标移动快捷键

    Linux 命令行快捷键 涉及在Linux命令行下进行快速移动光标.命令编辑.编辑后执行历史命令.Bang(!)命令.控制命令等.让basher更有效率. 常用 ctrl+左右键:在单词之间跳转 ct ...

  4. 把 Activity 改成 ListActivity继续使用 setContentView

    ListActivity has a default layout that consists of a single, full-screen list in the center of the s ...

  5. mysql如何查询日期的列表?

    转自:http://blog.csdn.net/liufei198613/article/details/72643345 select @num:=@num+1,date_format(adddat ...

  6. fiddler 面板内显示IP地址

    1.打开fiddler, 快捷键Ctrl+R  (菜单->Rules->Customize Rules…) 然后在CustomRules.js文件里Ctrl+F查找字符串:static f ...

  7. 修改js confirm alert 提示框文字

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. postgresql模式创建、修改、删除

    一个数据库包含一个或多个模式,而模式又包含表.序列.函数等,不同的模式可以包含相同名称的表.序列.函数等.模式本质上是命名空间,就像人的姓氏一样.一个用户只要有权限,连接到数据库后,可一次访问该数据库 ...

  9. Linux登录失败处理功能

     本文要实现的功能:如果有人恶意尝试破解你的服务器密码,那么这个功能就能帮你起到一定的作用,当尝试密码错误超过设定的次数后,就会锁定该账户多长时间(自行设定),时间过后即可自行解锁,这样可以增加攻击者 ...

  10. 了解MIP(Mobile Instant Pages)

    mip官网:https://www.mipengine.org/   什么是mip? mip是百度在2016年提出的移动网页加速器项目.可以简单理解为是一个规范.   mip能做什么? mip能帮助站 ...