【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 determine if a given target is in the array.

题目大意

  “在旋转数组中搜索值”的后续,如果数组中的值允许重复写一个程序确定一个给定的值是否在数组中。

解题思路

  先找最小的数所在的下标,再根据所要找的数字在哪一个部分进行查找。

代码实现

算法实现类

public class Solution {

    public boolean search(int[] nums, int target) {
        if (nums != null && nums.length > 0) {
            // 找最小元素对应的下标
            int minIndex = findMinIndex(nums);

            // 整个数组全局有序
            if (minIndex == 0) {
                return binarySearch(nums, 0, nums.length - 1, target);
            }
            // 有两个局部有序区间,  如 4 5 6 7 8 9 0 1 2 3
            else {
                // 恬好和后一个有序区间的最后一个元素相等,返回对应的下标
                if (nums[nums.length - 1] == target) {
                    return true;
                }
                // target可能在后一个有序区间中
                else if (nums[nums.length - 1] > target) {
                    return binarySearch(nums, minIndex, nums.length - 1, target);
                }
                // target可能是前一个有序区间中
                else {
                    return binarySearch(nums, 0, minIndex - 1, target);
                }
            }
        }

        return false;
    }

    /**
     * 二分搜索
     *
     * @param nums   数组
     * @param start  起始位置
     * @param end    结束位置
     * @param target 搜索目标
     * @return true找到,false没有找到
     */
    public boolean binarySearch(int[] nums, int start, int end, int target) {

        int mid;
        while (start <= end) {
            mid = start + ((end - start) >> 1);

            if (nums[mid] == target) {
                return true;
            } else if (nums[mid] > target) {
                end = mid - 1;
            } else {
                start = mid + 1;
            }
        }

        return false;
    }

    public int findMinIndex(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 sequenceSearchMinIndex(nums, lo, hi);
            }

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

        return mid;
    }

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

评测结果

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

特别说明

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

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

时间: 2024-07-31 19:08:13

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

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: 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. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

[LeetCode]题解(python):081 - Search in Rotated Sorted Array II

题目来源 https://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 a given

Java for LeetCode 081 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. 解题思路: 参考Java for LeetCode 033 Search in Rota

leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索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

081. Search in Rotated Sorted Array II

1 class Solution { 2 public: 3 bool search(vector<int>& nums, int target) { 4 if (nums.size() == 0) return false; 5 else { 6 int first = 0, last = nums.size() - 1; 7 while (first <= last) { 8 int mid = first + (last - first) / 2; 9 if (nums[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

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 determine if a given target is in the array. 题解: 这道题与之前Search in Rotated Sorted Array

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