一、题目描述

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.

这道题目就是Find Minimum in Rotated Sorted Array的增强版,在数组中增加了重复的元素。

具体的解法在http://www.cnblogs.com/rolly-yan/p/4032167.html中进行了详细的分析,如有需要请参考。

二、代码实现

package com.edu.leetcode;

public class FindMinimuminRotatedSortedArray {

	public int findMin(int[] num) {
int start=0;
int end=num.length-1; while(num[start]>=num[end]){ //num[start]>=num[end]说明数组不是正向有序的,最小值不在第一个位置 if(end-start==1){ //循环结束的条件,当只有两个元素的
return num[end];
}
int mid=(start+end)/2; if(num[start]==num[mid]&&num[mid]==num[end]){
//有这个条件有两种情况:1、当只有一个元素时;
//2、当数组中有大量重复的元素时,不可以再使用对半查找
int minValue=num[start];
for(int i=start+1;i<=end;i++){
if(num[i]<minValue)
minValue=num[i];
}
return minValue;
} if(num[mid]>=num[start]){ //当出现这种情况说明最小值出现在mid与end之间
start=mid;
}
else{ //说明最小出现在start与mid之间
end=mid;
}
}
return num[start]; //这个是说明数组从start到end正向有序,所以最小值就是start位置的元素
} public static void main(String[] args) {
// TODO Auto-generated method stub
FindMinimuminRotatedSortedArray f = new FindMinimuminRotatedSortedArray();
int[] num = { 1};
System.out.println(f.findMin(num));
}
}

  

【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现的更多相关文章

  1. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  2. LeetCode Find Minimum in Rotated Sorted Array II

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...

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

  4. LeetCode——Find Minimum in Rotated Sorted Array II

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

  5. [leetcode]Find Minimum in Rotated Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 解题思路:这道题和上一道题的区别是,数组中 ...

  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. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II)

    Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II) 假设按照升序排序的数组在预先未知的某个点上进 ...

  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 解题报告(Python)

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

随机推荐

  1. Android核心分析 之九Zygote Service

    Zygote Service 在本章我们会接触到这两个单词: Zygote [生物] 受精卵, 接合子, 接合体 Spawn:产卵 通过这两个单词,我们就可以大体知道Zygote是干什么的了,就是叫老 ...

  2. web.xml配置bug之提示The content of element type "web-app" must match "(icon?,display- name?,description?,distributable?,

    错误:配置web.xml时,出现红色叉叉,提示 The content of element type "web-app" must match "(icon?,disp ...

  3. Linux上的运行的jar包

    以调用json-simple为例 java程序(CsvTest.java) import org.json.simple.JSONObject; import java.util.*; public ...

  4. SQL Server 联表字段合并查询

    经常遇到统计报表中,子表记录合并为一个字段的情况.例如:省表中各省经济水平前五的城市统计. 有如下两表:dbo.省 和 dbo.市 (好吧,你可能会吐槽为什么用中文表名,其实我是为了方便查找替换) 这 ...

  5. js调用高德API获取所在当前城市

    可以在js代码中直接调用API接口,获取所处当前城市信息,代码如下: <script type="text/javascript"> function getCurre ...

  6. Python 中 os.path模板

    os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...

  7. Android内存管理(1)WRANGLING DALVIK: MEMORY MANAGEMENT IN ANDROID PART 1

    from : http://www.raizlabs.com/dev/2014/03/wrangling-dalvik-memory-management-in-android-part-1-of-2 ...

  8. POJ 1322 Chocolate(母函数)

    题目链接:http://poj.org/problem?id=1322 题意: 思路: double C[N][N]; void init() { C[0][0]=1; int i,j; for(i= ...

  9. 《OD学算法》排序

    参考 http://www.cnblogs.com/kkun/archive/2011/11/23/2260312.html http://blog.csdn.net/wuxinyicomeon/ar ...

  10. minimum-moves-to-equal-array-elements

    https://leetcode.com/problems/minimum-moves-to-equal-array-elements/ package com.company; import jav ...