`

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 become 4 5 6 7 0 1 2).

Find the minimum element.

The array may contain duplicates.

这道题目是[url] Find Minimum in Rotated Sorted Array[/url]的follow up, 唯一不同的是数组中多了重复元素。这时我们进行比较的时候就会出现相等的情况,如果相等我们就没法判断最小值是在哪半部分,我们只需要加一个相等时的处理,让左指针向前移动一个位置或右指针向后移动一个位置,直到找到不相等的两个元素,这样最坏的情况下就是元素都相等,时间复杂度为O(n)。代码如下:
public class Solution {
    public int findMin(int[] nums) {
        if(nums == null || nums.length == 0) 
            return -1;
        int l = 0;
        int r = nums.length - 1;
        while(l < r) {
            int m = l + (r - l) / 2;
            if(nums[m] > nums[r]) {
                l = m + 1;
            } else if(nums[m] < nums[r]) {
                r = m;
            } else {
                r --;
            }
        }
        return nums[l];
    }
}
分享到:
评论

相关推荐

    lrucacheleetcode-Coding-Interview:编程面试

    Find Minimum In Rotated Sorted Array Find Minimum In Rotated Sorted Array II Search In Rotated Sorted Array Search In Rotated Sorted Array II 二分搜索/有序矩阵 Kth Smallest Element In A Sorted Matrix ...

    leetcode分类-interview:面试基础算法

    sorted array 81: search in rotated sorted array ii 153: find minimum in rotated sorted array 162: find peak element 34: find first and last position of element in sorted array 300: longest increasing ...

    cpp-算法精粹

    Find Minimum in Rotated Sorted Array II Median of Two Sorted Arrays H-Index II 暴力枚举法 Subsets Subsets II Permutations Permutations II Combinations Letter Combinations of a Phone Number 广度优先...

    leetcode写题闪退-LeetCode:leetcodeOJ

    II 和 Find Minimum in Rotated Sorted Array这两道题目,我也是醉了。。。代码完全一样的不说,题目还都特别傻逼。 Maximum Product Subarray 动态规划,类似于求最大连续和,但这里由于是乘法,还要考虑符号问题。...

    javalruleetcode-leetcode:这是Java写的leetcode

    [Java](./src/Find minimum in Rotated Sorted Array II/Solution.java) 2014/10/20 难的 [Java](./src/在旋转排序数组中查找最小值/Solution.java) 2014/10/15 中等的 [Java](./src/最大乘积子阵列/Solution.java) ...

    LeetCode C++全解

    Find Minimum in Rotated Sorted Array viii. Largest Rectangle in Histogram ix. Maximal Rectangle x. Palindrome Number xi. Search a 2D Matrix xii. Search for a Range xiii. Search Insert Position xiv. ...

    扔鸡蛋leetcode-LeetCode-Note-Mary:玛丽的leetcode笔记本

    扔鸡蛋 leetcode LeetCode-Note-Mary Mary's leetcode notebook The order of problems ...in ...Minimum in Rotated Sorted Array(寻找旋转排序数组中的最小值) 2020/12/09 300. Longest Increasing

    Find-Minimum-in-Rotated-Sorted-Array-II

    查找最小的旋转排序数组II 假设以升序排序的长度为n的数组在1到n之间旋转。 例如,数组nums = [0,1,4,4,5,6,7]可能变为: [4,5,6,7,0,1,4]如果旋转了4次。 [0,1,4,4,5,6,7]如果已旋转7次。 请注意,旋转数组[a ...

    Leetcode扑克-coding-interviews:编码面试

    II 替换空格 从尾到头打印链表 重建二叉树 105. Construct Binary Tree from Preorder and Inorder Traversal 用两个栈实现队列 232. Implement Queue using Stacks 旋转数组的最小数字 153. Find Minimum in ...

    leetcodelintcode差异-leetcode-python:leetcode-python

    Find Minimum in Rotated Sorted Array 双指针 题目 Solution Tag LintCode 604. Window Sum LeetCode 283. Moves Zeroes Array、Two Pointers Array、Two Pointers LeetCode 120. Triangle LintCode 382. Triangle ...

    leetcode变形词-Leetcode-Data-Structures-and-Algorithms:该存储库包含基于数据结构和算法的编码问

    leetcode变形词 :fire: Leetcode / 数据结构和算法 ...in_Rotated_Sorted_Array.java) :pushpin: 数组 :pushpin: 比赛 提交 2020 年 Google Code Jam 资格赛 提交 Google Hash Code 2020 在线资格赛

    lrucacheleetcode-leetcode-in-go:leetcode-in-go

    find-minimum-in-rotated-sorted-array-0153 数组的乘积-除了-self-0238 从排序数组中删除重复项-0026 搜索旋转排序数组-0033 两个整数之和-0371 二和-0001 回溯 组合-和-0039 组合总和-ii-0040 电话号码的字母组合 ...

    javalruleetcode-shy:害羞的

    minimum number in array given a sorted array, rotated at a pivot, find the maximum element what is encapsulation(封装) 大部分时间在问general behavior question。 比如要是组里有人不工作怎么办, 工作如果...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - ADD: In the module FlexUtils added the constant BooleanWords - an array containing the words 'false' and 'true' for saving/reading TBoolProp. - ADD: In the module FlexUtils added the function ...

Global site tag (gtag.js) - Google Analytics