首页 > 其他 > 详细

Leetcode#154Find Minimum in Rotated Sorted Array II

时间:2015-05-21 06:44:37      阅读:263      评论:0      收藏:0      [点我收藏+]

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


Show Tags

分析:如果没有重复的可以直接比较就可以中间项和末尾项即可区别出使用哪一个片段,此题说明是重复项存在,因此除非极端情况下可,二分取其一处理外,其他情况两部分都要处理


例如,一种情况

3 3 3 3 3 1 3


public class Solution {

    int fm(int[] nums, int start, int end){

        int l=end-start+1;

        if(l==1)

            return nums[start];

        else {

            int s=start;

            int e=end;

            int m=(s+e)/2;

            int min=0;

            if(nums[m]<nums[e])

                min=fm(nums, s, m);

            else

            {

                int m1=fm(nums, s, m);

                min=fm(nums, m+1, e);

                if(m1<min)

                    min=m1;

            }

            return min;

        }    

    }

    

    public int findMin(int[] nums) {

        int x=fm(nums,0,nums.length-1);

        return x;

    }

}


Leetcode#154Find Minimum in Rotated Sorted Array II

原文:http://7061299.blog.51cto.com/7051299/1653326

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!