Skip to content

Latest commit

 

History

History

33

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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
  • -104 <= nums[i] <= 104
  • All values of nums are unique.
  • nums is an ascending array that is possibly rotated.
  • -104 <= target <= 104

Companies: Amazon, Apple, Google

Related Topics:
Array, Binary Search

Similar Questions:

Solution 1.

Use the solution to 153. Find Minimum in Rotated Sorted Array (Medium).

We first find the pivot point, then we can do a normal binary search on [0, pivot - 1] or [pivot, N - 1].

// OJ: https://leetcode.com/problems/search-in-rotated-sorted-array/
// Author: github.com/lzl124631x
// Time: O(logN)
// Space: O(1)
class Solution {
public:
    int search(vector<int>& nums, int target) {
        if (nums.empty()) return -1;
        int L = 0, R = nums.size() - 1, pivot;
        while (L < R) {
            int M = L + (R - L) / 2;
            if (nums[M] < nums[R]) R = M;
            else L = M + 1;
        }
        pivot = L;
        if (pivot && target >= nums[0] && target <= nums[pivot - 1]) L = 0, R = pivot - 1;
        else L = pivot, R = nums.size() - 1;
        while (L <= R) {
            int M = L + (R - L) / 2;
            if (nums[M] == target) return M;
            if (target > nums[M]) L = M + 1;
            else R = M - 1;
        }
        return -1;
    }
};

Solution 2.

Similar to the Solution 1, but we shift the M by pivot to find the real mid point (need to % N).

// OJ: https://leetcode.com/problems/search-in-rotated-sorted-array/
// Author: github.com/lzl124631x
// Time: O(logN)
// Space: O(1)
// https://discuss.leetcode.com/topic/3538/concise-o-log-n-binary-search-solution
class Solution {
public:
    int search(vector<int>& nums, int target) {
        if (nums.empty()) return -1;
        int N = nums.size(), L = 0, R = N - 1, pivot;
        while (L < R) {
            int M = L + (R - L) / 2;
            if (nums[M] < nums[R]) R = M;
            else L = M + 1;
        }
        pivot = L;
        L = 0, R = N - 1;
        while (L <= R) {
            int M = L + (R - L) / 2;
            int MM = (M + pivot) % N;
            if (nums[MM] == target) return MM;
            if (target > nums[MM]) L = M + 1;
            else R = M - 1;
        }
        return -1;
    }
};

Solution 3.

Let L = 0, R = N - 1, M = (L + R) / 2.

  • If A[M] == target, return M
  • If A[M] > A[R], the min point in the array is to the right of A[M], we then compare target with A[M] or A[L] to see if we should move L or R.
  • If A[M] <= A[R], the min point in the array is to the left of A[M], we can then compare target with A[M] or A[R] to see if we should move L or R.
// OJ: https://leetcode.com/problems/search-in-rotated-sorted-array/
// Author: github.com/lzl124631x
// Time: O(logN)
// Space: O(1)
class Solution {
public:
    int search(vector<int>& A, int target) {
        int N = A.size(), L = 0, R = N - 1;
        while (L <= R) {
            int M = (L + R) / 2;
            if (A[M] == target) return M;
            if (A[M] > A[R]) {
                if (target >= A[L] && target < A[M]) R = M - 1;
                else L = M + 1;
            } else {
                if (target > A[M] && target <= A[R]) L = M + 1;
                else R = M - 1;
            }
        }
        return -1;
    }
};

We can simplify it by combining the cases.

// OJ: https://leetcode.com/problems/search-in-rotated-sorted-array/
// Author: github.com/lzl124631x
// Time: O(logN)
// Space: O(1)
class Solution {
public:
    int search(vector<int>& A, int target) {
        int L = 0, R = A.size() - 1;
        while (L <= R) {
            int M = (L + R) / 2;
            if (A[M] == target) return M;
            if ((A[M] > A[R] && (target > A[M] || target < A[L]))
               || (A[M] <= A[R] && (target > A[M] && target <= A[R]))) L = M + 1;
            else R = M - 1;
        }
        return -1;
    }
};