Find Minimum in Rotated Sorted Array II 旋转数组中找最小值(有重复元素) @LeetCode

递归

public class Solution {
    public int findMin(int[] num) {
        return helper(num, 0, num.length-1);
    }

    //with duplicate
    public static int helper(int[] a, int left, int right){

        //one element
        if(left == right){
            return a[left];
        }

        //two elements
        if(left == right-1){
            return a[left]<a[right]? a[left]: a[right];
        }

        //the array is ordered
        if(a[left] < a[right]){
            return a[left];
        }

        int mid = (left+right)/2;

        if(a[mid] > a[left]){
            return helper(a, mid, right);
        }else if(a[mid] < a[left]){
            return helper(a, left, mid);
        }else{ //when a[mid] == a[left], we have to search both side
            return Math.min(helper(a, mid, right), helper(a, left, mid));
        }

    }
}
时间: 2024-11-10 15:37:42

Find Minimum in Rotated Sorted Array II 旋转数组中找最小值(有重复元素) @LeetCode的相关文章

【LeetCode】Find Minimum in Rotated Sorted Array 在旋转数组中找最小数

Add Date 2014-10-15 Find Minimum in Rotated Sorted Array 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

[LeetCode]111. Find Minimum in Rotated Sorted Array II旋转数组最小值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

【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 寻找旋转有序数组的最小值 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

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

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 (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