leetcode------Find Minimum in Rotated Sorted Array II

标题: Find Minimum in Rotated Sorted Array II
通过率: 31.1%
难度:

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可以直接点击去看我对于旋转数组的分析。本题直接看代码

 1 public class Solution {
 2     public int findMin(int[] num) {
 3         int start=0,end=num.length-1,mid=0;
 4         while(start<end&&num[start]>=num[end]){
 5             mid=(start+end)/2;
 6             if(num[mid]>num[end])start=mid+1;
 7             else if(num[mid]<num[end])end=mid;
 8             else start+=1;
 9         }
10         return num[start];
11     }
12 }

 

posted @ 2015-02-15 11:10  pku_smile  阅读(162)  评论(0编辑  收藏  举报