Wednesday, April 26, 2017

[LintCode] 159 Find Minimum in Rotated Sorted Array 解题报告

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


Notice
You may assume no duplicate exists in the array.



Example
Given [4, 5, 6, 7, 0, 1, 2] return 0



思路
数组里最小的值一定比数组最后那个数字target = nums[nums.length - 1]的值小,或者想等。
那么我们只要找数组里第一个小于等于target的值。




Code
public class Solution {
    /**
     * @param nums: a rotated sorted array
     * @return: the minimum number in the array
     */
    public int findMin(int[] nums) {
        // write your code here
        
        int start = 0;
        int end = nums.length - 1;
        int target = nums[end];
        while (start + 1 < end) {
            int mid = start + (end - start) / 2;
            if (nums[mid] > target) {
                start = mid;
            }
            else {
                end = mid;
            }
        }
        
        return Math.min(nums[start], nums[end]);
    }
}a