码迷,mamicode.com
首页 > 编程语言 > 详细

Find Minimum in Rotated Sorted Array II 旋转数组中找最小值(有重复元素) @LeetCode

时间:2014-11-11 12:40:09      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:blog   io   ar   java   on   2014   log   ef   as   

递归


public class Solution {
    public int findMin(int[] num) {
        return helper(num, 0, num.length-1);
    }
    
    //with duplicate
    public static int helper(int[] a, int left, int right){
         
        //one element
        if(left == right){
            return a[left];
        }
         
        //two elements
        if(left == right-1){
            return a[left]<a[right]? a[left]: a[right];
        }
         
        //the array is ordered
        if(a[left] < a[right]){
            return a[left];
        }
         
        int mid = (left+right)/2;
         
        if(a[mid] > a[left]){
            return helper(a, mid, right);
        }else if(a[mid] < a[left]){
            return helper(a, left, mid);
        }else{ //when a[mid] == a[left], we have to search both side
            return Math.min(helper(a, mid, right), helper(a, left, mid));
        }
         
    }
}





Find Minimum in Rotated Sorted Array II 旋转数组中找最小值(有重复元素) @LeetCode

标签:blog   io   ar   java   on   2014   log   ef   as   

原文地址:http://blog.csdn.net/fightforyourdream/article/details/41009717

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!