[LeetCode]25. Search in Rotated 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).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.

解法1:顺序查找,时间复杂度O(n)。

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int n = nums.size(), i = 0;
        while(i < n && target != nums[i])
            i++;
        return i == n ? -1 : i;
    }
};

解法2:旋转数组是分为两个有序数组,因此可以使用二分查找。若数组首元素小于数组尾元素,则数组没有旋转,直接使用二分查找binarySearch即可;否则(1)初始化left=0,right=n-1,取mid=(left+right)/2;(2)如果target==nums[mid],则直接返回mid;否则若nums[left]<nums[mid],说明nums[left, ..., mid-1]是有序的,而nums[mid+1, ... , n-1]是旋转的,对前者调用binarySearch,若没找到再对后者调用search;若nums[left]>nums[mid],说明nums[left, ..., mid-1]是旋转的,而nums[mid+1, ... , n-1]是有序的,对后者调用binarySearch,若没找到再对前者调用search。

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int n = nums.size();
        if (n < 2)
            return n == 1 ? (target == nums[0] ? 0 : -1) : -1;

        int res, left = 0, right = n - 1;
        if (nums[left] < nums[right])
            res = binarySearch(nums, left, right, target);
        else
        {
            int mid = (left + right) >> 1;
            if (target == nums[mid])
                res = mid;
            else if (nums[left] < nums[mid])
            {
                if ((res = binarySearch(nums, left, mid - 1, target)) == -1)
                {
                    vector<int> tmp(nums.begin() + mid + 1, nums.end());
                    res = search(tmp, target);
                    res = res == -1 ? -1 : res + mid + 1;
                }
            }
            else
            {
                if ((res = binarySearch(nums, mid + 1, right, target)) == -1)
                {
                    vector<int> tmp(nums.begin(), nums.begin() + mid);
                    res = search(tmp, target);
                }
            }
        }
        return res;
    }
private:
    int binarySearch(vector<int>& nums, int left, int right, int key)
    {
        if (left > right)
            return -1;

        int mid = (left + right) >> 1;
        if (key == nums[mid])
            return mid;
        else if (key > nums[mid])
            return binarySearch(nums, mid + 1, right, key);
        else
            return binarySearch(nums, left, mid - 1, key);
    }
};

上面的代码可以进一步优化。通过target和nums[left]、nums[right]、nums[mid]进行比较可以去除接下来一些不必要的查找。对于nums[left]>nums[mid],若target>nums[mid]&&target>nums[right],则下一步只需在nums[left,...,mid-1]中调用search查找;若target>nums[mid]&&target<=nums[right],则下一步只需在nums[mid+1,...,right]中调用binarySearch查找;若target<nums[mid],则下一步只需在nums[left,...,mid-1]中调用search查找。对于nums[left]<nums[mid],若target<nums[mid]&&target<nums[left],则只需在nums[mid+1,...,right]中调用search查找;若target<nums[mid]&&target>=nums[left],则只需在nums[left,...,mid-1]中调用binarySearch查找;若target>nums[mid],则只需在nums[mid+1,...,right]中调用search查找。

时间: 2024-08-15 13:49:48

[LeetCode]25. Search in Rotated Array旋转数组查找的相关文章

[LeetCode]26. Search in Rotated 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. 解法:同旋转数组查找I,本题也使用二分查找,只是在nums[mid]和nums[right

[LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. Search in Rotated Sorted Array (Hard) 链接: 题目:https://leetcode.com/problems/search-in-rotated-sorted-array/ 代码(github):https://github.com/illuz/leetcode 题

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 33 Search in Rotated Sorted Array [binary search] &lt;c++&gt;

LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前半部分接到后半部分后面,得到一个新数组,在新数组中查找给定数的下标,如果没有,返回-1.时间复杂度限制\(O(log_2n)\) C++ 我的想法是先找到数组中最大值的位置.然后以此位置将数组一分为二,然后在左右两部分分别寻找target. 二分寻找最大值的时候,因为左半部分的数一定大于nums[l],所以n

LeetCode Solutions : 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 Solution ( refer to my blog LeetCode So

[Lintcode]62. Search in Rotated Sorted Array/[Leetcode]33. Search in Rotated Sorted Array

[Lintcode]62. Search in Rotated Sorted Array/[Leetcode]33. Search in Rotated Sorted Array 本题难度: Medium/Medium Topic: Binary Search Description Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (

【leetcode】Search in Rotated Sorted Array II(middle)☆

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. 我的思路: 太混乱了 不提了.注意关键区分依据 排好序的一定是从小到大的 看大神的吧: b

leetcode旋转数组查找 二分查找的变形

http://blog.csdn.net/pickless/article/details/9191075 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. If found in the array return it

(每日算法)LeetCode --- Search in Rotated Sorted Array(旋转数组的二分检索)

Search in Rotated Sorted Array I && II Leetcode 对有序数组进行二分查找(下面仅以非递减数组为例): int binarySort(int A[], int lo, int hi, int target) { while(lo <= hi) { int mid = lo + (hi - lo)/2; if(A[mid] == target) return mid; if(A[mid] < target) lo = mid + 1;