lintcode-medium-Find Minimum in Rotated Sorted Array II

Posted 哥布林工程师

tags:

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

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.

 

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

public class Solution {
    /**
     * @param num: a rotated sorted array
     * @return: the minimum number in the array
     */
    public int findMin(int[] num) {
        // write your code here
        if(num == null || num.length == 0)
            return 0;
        
        int left = 0;
        int right = num.length - 1;
        
        while(left < right - 1){
            if(num[left] < num[right])
                return num[left];
            
            while(left < right - 1 && num[left] == num[left + 1])
                left++;
            while(left < right - 1 && num[right] == num[right - 1])
                right--;
            
            int mid = left + (right - left) / 2;
            
            if(num[left] <= num[mid])
                left = mid + 1;
            else
                right = mid;
        }
        
        return Math.min(num[left], num[right]);
    }
}

 

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

lintcode-medium-Find Minimum in Rotated Sorted Array II

[Mini2440] 使用 miniTool 给 mini2440 刷机

Arduino mini pro对多能烧录多少字节?

小米全新路由器与小米mini哪个好

mini ui 中mini.parse 语句的作用?

MVC-Mini-Profiler - Web 表单 - 找不到 /mini-profiler-results