LeetCode 33. Search in Rotated Sorted Array

2021/04/17 leetCode 共 2439 字,约 7 分钟

There is an integer array nums sorted in ascending order (with distinct values).

Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].

Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.

You must write an algorithm with O(log n) runtime complexity.

Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
Example 2:
Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1
Example 3:
Input: nums = [1], target = 0
Output: -1

Constraints:

  • 1 <= nums.length <= 5000
  • -$10^4$ <= nums[i] <= $10^4$
  • All values of nums are unique.
  • nums is an ascending array that is possibly rotated.
  • $10^4$ <= target <= $10^4$

分析: 这道题采用分治法. 由于经过了反转, nums不是有序的, 因此首先要找到旋转中心.

int findRotIndex(vector<int> const &nums) {
    int left = 0;
    int right = nums.size()-1;
    
    while (left <= right) {
        int mid = left + (right-left)/2;
        int rightVal = nums[mid+1];
        if (rightVal < nums[mid]) {
            return mid;
        }
        // 10 11 12 0 1 2 3
        if (nums[mid] > nums[left]) {
            left = mid+1;
        } else {
            right = mid;
        }
    }
    return -1;
}

接下来可以判断target旋转中心左边还是右边序列, 采用二分查找进行搜索

int binarySearch(vector<int> const &nums, int left, int right, int const target) {
    while (left <= right) {
        int mid = left + (right-left)/2;
        if (nums[mid] == target) return mid;
        
        if (target > nums[mid]) {
            left = mid+1;
        } else {
            right = mid-1;
        }
    }
    return -1;
}

本题的答案:

class Solution {
public:
    int search(vector<int>& nums, int target) {
        if (nums.front() <= nums.back()) {
            // strictly increasing order
            return binarySearch(nums, 0, nums.size()-1, target);
        }
        
        int const rotIdx = findRotIndex(nums);
        
        // std::cout << "rot idx: " << rotIdx << " val: " << nums[rotIdx] << std::endl;
        
        if (target == nums[rotIdx]) return rotIdx;
        
        if (target < nums.front()) { // not a comparison against the value at the rotation index!
            return binarySearch(nums, rotIdx+1, nums.size()-1, target);
        } else {
            return binarySearch(nums, 0, rotIdx-1, target);
        }
        
        return -1;
    }
    
    int binarySearch(vector<int> const &nums, int left, int right, int const target) {
        while (left <= right) {
            int mid = left + (right-left)/2;
            if (nums[mid] == target) return mid;
            
            if (target > nums[mid]) {
                left = mid+1;
            } else {
                right = mid-1;
            }
        }
        return -1;
    }
    
    int findRotIndex(vector<int> const &nums) {
        int left = 0;
        int right = nums.size()-1;
        
        while (left <= right) {
            int mid = left + (right-left)/2;
            int rightVal = nums[mid+1];
            if (rightVal < nums[mid]) {
                return mid;
            }
            // 10 11 12 0 1 2 3
            if (nums[mid] > nums[left]) {
                left = mid+1;
            } else {
                right = mid;
            }
        }
        return -1;
    }
};

文档信息

Search

    Table of Contents