[LeetCode] Find Minimumin Rotated Sorted Array II

题意:在旋转过的有序数组中找到最小数,数组中可能有重复元素

思路:二分,判断是否有相等的元素,主要是二分的时候的一些细节,比如说是有序的还是rotated

代码:

    public int findMin(List<Integer> nums) {//solution1 O(log(N))
        int min = Integer.MAX_VALUE;
        int l = 0, r = nums.size()-1,mid;

        while (l < r){
            if(l + 1 == r){
                min = Math.min(min, Math.min(nums.get(l),nums.get(r)));
                return min;
            }
            mid = r + (l - r) / 2;
            if(nums.get(l) == nums.get(r)){//重复数据
                l ++;
                continue;
            }else if(nums.get(l) < nums.get(r)){
                min = nums.get(l);
                return min;
            }else {
                if(nums.get(mid) == nums.get(r)){//表明右边的数据都相等
                    r = mid;
                }else if(nums.get(mid) > nums.get(r)){
                    l = mid;
                }else if(nums.get(mid) < nums.get(r)){
                    r = mid;
                }
            }
        }
        min = Math.min(min, nums.get(l));
        return min;

    }

时间: 2024-10-06 00:01:50

[LeetCode] Find Minimumin Rotated Sorted Array II的相关文章

[leetcode]Search in Rotated Sorted Array II @ Python

原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if

[LeetCode] Search in Rotated Sorted Array II [36]

题目 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 原题链接(点我) 解题思路 这题和Search in Rotated Sorted

LeetCode——Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 原题链接:https://oj.leetcode.com/problems/search

LeetCode: Search in Rotated Sorted Array II [081]

[题目] Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. [题意] 在"Search in Rotated Sorted Ar

LeetCode: Search in Rotated Sorted Array II 解题报告

Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the arr

Leetcode#81Search in Rotated Sorted Array II

Search in Rotated Sorted Array II Total Accepted: 35100 Total Submissions: 111308My Submissions Question Solution Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and w

leetcode:Search in Rotated Sorted Array II(duplicated)

题目要求: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 题目地址:https://leetcode.com/problems/sear

[leetcode]Search in Rotated Sorted Array II

问题描述: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. "Search in Rotated Sorted Array&q

[LeetCode] Search in Rotated Sorted Array II 二分搜索

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. Hide Tags Array Binary Search 这个是在 前一道的基础上改过来