Find Minimum in Rotated Sorted Array

 Total Accepted: 42341 Total Submissions: 127863

Question Solution 

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.

You may assume no duplicate exists in the array.

Show Tags

分析:从有序但被旋转的数组中查找最小数,遍历可以查找但时间花销太大,采用二分查找法

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

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

            return min;

        }    

    }

    

    public int findMin(int[] nums) {

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

        return x;

    }

    

}