lintcode159- Find Minimum in Rotated Sorted Array- medium

Posted jasminemzy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lintcode159- Find Minimum in Rotated Sorted Array- medium相关的知识,希望对你有一定的参考价值。

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

 

OOXX二分法(find the first X)。特征:相对最后一个数的大小。O:>nums[end]; X: <=nums[end]。

切记要和最后一个数比,不可以和第一个数比,因为和第一个数比在没有rotate过的特殊情况里会找空。

 

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 cmpTarget = nums[end];

        while (start + 1 < end){
            int mid = start + (end - start) / 2;
            if (nums[mid] > cmpTarget){
                start = mid;
            } else {
                end = mid;
            }
        }

        return Math.min(nums[start], nums[end]);
    }
}

 

以上是关于lintcode159- Find Minimum in Rotated Sorted Array- medium的主要内容,如果未能解决你的问题,请参考以下文章

LintCode : Find Minimum in Rotated Sorted Array

lintcode-medium-Find Minimum in Rotated Sorted Array II

lintcode160- Find Minimum in Rotated Sorted Array II- medium

Lintcode: Interval Minimum Number

lintcode-medium-Minimum Adjustment Cost

lintcode-medium-Minimum Window Substring