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]);
    }
}
时间: 2024-10-17 21:29:11

lintcode-medium-Find Minimum in Rotated Sorted Array II的相关文章

LeetCode Find Minimum in Rotated Sorted Array II

Find Minimum in Rotated Sorted Array II Total Accepted: 23090 Total Submissions: 73108 My Submissions Question Solution Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexi

Leetcode#154Find Minimum in Rotated Sorted Array II

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

LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法

Find Minimum in Rotated Sorted Array II 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 be

leetcode -Find Minimum in Rotated Sorted Array II (1)

本人大三狗,大一学物理,大二转专业来了计院.一入计院深似海,从此节操是路人.转眼间一年过去了,基本上课本的知识学的很好,考前突击分数还很光鲜,但是总是觉得空虚.因为在这个讲究技术的年代,没有一点技术压身,是很容易睡不着觉的.近日阅读了不少前人的经验教训,感觉自己的目标很明确,应届入bat,有必要考个研也没问题,保研估计没戏了.在这个讲究实战的年代,我有必要积累一点代码行数了,否则坑定是混不过面试的.而且还自以为是地定制了一批书单,现在看到堆到50cm搞的一堆书,也觉得压力山大.我就是属于这种书看

【LeetCode】Find Minimum in Rotated Sorted Array II (2 solutions)

Find Minimum in Rotated Sorted Array II 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

leetcode_154_Find Minimum in Rotated Sorted Array II

欢迎大家阅读参考,如有错误或疑问请留言纠正,谢谢 154 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 duplica

【LeetCode-面试算法经典-Java实现】【155-Find Minimum in Rotated Sorted Array II(找旋转数组中的最小数字II)】

[154-Find Minimum in Rotated Sorted Array II(找旋转数组中的最小数字II)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Supp

Leetcode[154]-Find Minimum in Rotated Sorted Array II

Link: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 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

[leedcode 154] Find Minimum in Rotated Sorted Array II

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 be

Find Minimum in Rotated Sorted Array II

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 be