【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?

  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.

题目大意

   “查找旋转排序的数组最小值”的后续:允许重复元素,但是两个字数组依然局部有序。

解题思路

  采用类二分搜索算法进行查找

代码实现

算法实现类

public class Solution {

    public int findMin(int[] nums) {
        // 参数校验
        if (nums == null || nums.length < 1) {
            throw new IllegalArgumentException();
        }

        int lo = 0;
        int hi = nums.length - 1;
        int mid = 0;

        // 可以排除数组全局有序的情况
        while (nums[lo] >= nums[hi]) {

            // 如果只有两个元素,返回后一个
            if (hi - lo == 1) {
                mid = hi;
                break;
            }

            mid = lo + ((hi - lo) >> 1);

            if (nums[mid] == nums[lo] && nums[mid] == nums[hi]) {
                // 只能采用顺序搜索方法,不能采用lo++,hi--的方式
                // 因为lo可能是前一个有序数组的最后一个
                // hi也可能是后一个有序数组的第一个
                return sequenceSearch(nums, lo, hi);
            }

            // 如果mid在前一个有序数组中
            if (nums[mid] >= nums[lo]) {
                lo = mid;
            }
            // 如果mid在后一个有序数组中
            else if (nums[mid] <= nums[hi]) {
                hi = mid;
            }

        }

        return nums[mid];
    }

    /**
     * 顺序搜索数组中的最小值,nums是由有序数组按某个轴旋转得来的
     *
     * @param nums  搜索数组
     * @param start 开始位置
     * @param end   结束位置
     * @return 最小值
     */
    public int sequenceSearch(int[] nums, int start, int end) {
        for (int i = start; i < end; i++) {
            if (nums[i] > nums[i + 1]) {
                return nums[i + 1];
            }
        }
        return nums[start];
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47828189

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-09 17:24:28

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

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

[153-Find Minimum in Rotated Sorted Array(找旋转数组中的最小数字)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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 m

【LeetCode-面试算法经典-Java实现】【033-Search in Rotated Sorted Array(在旋转数组中搜索)】

[033-Search in Rotated Sorted Array(在旋转数组中搜索)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 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). You are given a target value to search.

【LeetCode-面试算法经典-Java实现】【026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)】

[026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for anot

【LeetCode-面试算法经典-Java实现】【081-Search in Rotated Sorted Array II(搜索旋转的排序数组)】

[081-Search in Rotated Sorted Array II(搜索旋转的排序数组)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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

新手算法学习之路----二分法Find Minimum in Rotated Sorted Array

题目:假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2). 你需要找到其中最小的元素. 你可以假设数组中不存在重复的元素. 思路:首先排除三种极端情况,空,只有一个元素,以及整个数组都是顺序排列的. 当顺序的数组随机旋转排列后,就分为两个顺序列入题目中的4567和012,寻找到中间数来和数组最后一个元素对比,如果大于的话说明最小的数在中间数的右边,如果小于的话说明最小数在中间数的左边,然             后继续按照二分法来找.

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

【leetcode】Find Minimum in Rotated Sorted Array II 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? Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 m

[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

Find Minimum in Rotated Sorted Array II -- LeetCode

这道题是Search in Rotated Sorted Array的扩展,思路在Find Minimum in Rotated Sorted Array中已经介绍过了,和Find Minimum in Rotated Sorted Array唯一的区别是这道题目中元素会有重复的情况出现.不过正是因为这个条件的出现,影响到了算法的时间复杂度.原来我们是依靠中间和边缘元素的大小关系,来判断哪一半是不受rotate影响,仍然有序的.而现在因为重复的出现,如果我们遇到中间和边缘相等的情况,我们就无法判