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

一、题目描述

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));
	}
}

  

时间: 2024-10-18 10:50:19

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

leetcode -Find Minimum in Rotated Sorted Array II (1)

本人大三狗,大一学物理,大二转专业来了计院.一入计院深似海,从此节操是路人.转眼间一年过去了,基本上课本的知识学的很好,考前突击分数还很光鲜,但是总是觉得空虚.因为在这个讲究技术的年代,没有一点技术压身,是很容易睡不着觉的.近日阅读了不少前人的经验教训,感觉自己的目标很明确,应届入bat,有必要考个研也没问题,保研估计没戏了.在这个讲究实战的年代,我有必要积累一点代码行数了,否则坑定是混不过面试的.而且还自以为是地定制了一批书单,现在看到堆到50cm搞的一堆书,也觉得压力山大.我就是属于这种书看

LeetCode Find Minimum in Rotated Sorted Array II

Find Minimum in Rotated Sorted Array II Total Accepted: 23090 Total Submissions: 73108 My Submissions Question Solution Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexi

Leetcode#154Find Minimum in Rotated Sorted Array II

Find Minimum in Rotated Sorted Array II Total Accepted: 25678 Total Submissions: 80978My Submissions Question Solution Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity

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 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 be

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

原题地址:https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 解题思路:这道题和上一道题的区别是,数组中可能有相同的数.那么,分下列几种情况: 代码: class Solution: # @param num, a list of integer # @return an integer def findMin(self, num): L = 0; R = len(num)-1 while L < R

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 might become 4 5 6 7 0 1 2). Find the minimum element. The array may contain duplicates. 解题思路: 参考Java for LeetCode 081 Search in Rotated Sorted Array II J

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

LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法

Find Minimum in Rotated Sorted Array II 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 be

【LeetCode】Find Minimum in Rotated Sorted Array II (2 solutions)

Find Minimum in Rotated Sorted Array II 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