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 a sorted array 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.

接着153题,在数组中有重复的数字。

只需要判断一下当nums[left] == nums[mid] 即可。

public class Solution {
public int findMin(int[] nums) { if( nums.length == 1)
return nums[0];
int left = 0,right = nums.length-1;
int mid = (left+right)/2;
int first = nums[0]; while( left < right ){ if( left == right-1 ){
left++;
}
else if( nums[mid] == nums[left]){
left++;
}else if( nums[left] < nums[mid] ){
left = mid;
}else{
right = mid;
}
mid = (left+right)/2;
first = Math.min(first,nums[left]); }
return Math.min(first,nums[left]);
}
}

leetcode 154. Find Minimum in Rotated Sorted Array II --------- java的更多相关文章

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

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

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

  4. [LeetCode#154]Find Minimum in Rotated Sorted Array II

    The question: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are ...

  5. LeetCode 154. Find Minimum in Rotated Sorted Array II寻找旋转排序数组中的最小值 II (C++)

    题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...

  6. 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现

    一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...

  7. LeetCode 154.Find Minimum in Rotated Sorted Array II(H)(P)

    题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...

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

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

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

随机推荐

  1. 酷狗音乐盒缓存文件夹KuGouCache的设置方法

    1.每次一打开酷狗总能在E盘里找到这个 KuGouCache 文件夹 ,是自动生成的MV缓存文件 .按照常规 ,可以修改这个文件的办法是 找到C盘里的:用户\administrator\AppDate ...

  2. Hibernate缓存机制 (2013-07-02 13:51:32)转载▼

    标签: java web hibernate 缓存 代码        分类: javaweb 缓存是位于应用程序与物理数据源之间,用于临时存放复制数据的内存区域,目的是为了减少应用程序对物理数据源访 ...

  3. MATLAB图像处理函数汇总(二)

    60.imnoise 功能:增加图像的渲染效果. 语法: J = imnoise(I,type) J = imnoise(I,type,parameters) 举例 I = imread('eight ...

  4. [转]理解WSRF之一 使用WS-ResourceProperties (整理自IBM网站)

    理解 WSRF第1部分-使用 WS-ResourceProperties   本 教程是一个由 4 部分组成的系列文章中的第 1 部分,该系列介绍 WSRF(Web Services Resource ...

  5. word表格从 web版视图 转 到页面视图 模式后 表格的一部分显示不出来

    解决办法 在web视图 中选择 表格  ----->右键  表格属性  表格 下面 的  文字环绕  选择 无

  6. hdu 2089

    PS:额...暴力打表...今天学到的新名词..记得把数组开到100W.. 代码: #include "stdio.h" ]; int cal(int a); int main() ...

  7. sql 字段重复值,in,like

  8. Design Patterns---- Strategy 模式

    设计模式:可复用面向对象软件的基础 书中对 Strategy 模式的定义如下: 定义了一系列的算法,把它们一个个封装起来,并且使它们可相互替换.本模式使得算法可独立于它的用户而变化. 案例:设计一个商 ...

  9. HDOJ-三部曲-1002-Radar Installation

    Radar Installation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) ...

  10. linux 任务调度 系统任务调度

    linux  at 针对运行一次的任务 crontab   控制计划任务的命令 crond系统服务 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程, 与windows ...