This article provides a complete breakdown of the solution to LeetCode 33 Search in Rotated Sorted Array: from the problem-solving approach and algorithm steps to time and space complexity, and it also summarizes boundary cases and compares three solutions, helping you understand Search in Rotated Sorted Array in one go.
Explore diverse LeetCode solutions in Python, C++, JavaScript, SQL, and TypeScript. Ideal for interview prep, learning, and code practice in multiple programming languages. Github Repo Link
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
numsare unique. numsis an ascending array that is possibly rotated.-10 4 <= target <= 10 4
Python
from typing import List class Solution: def search(self, nums: List[int], target: int) -> int: # return nums.index(target) if target in nums else -1 n = len(nums) left, right = 0, n-1 while left <= right: mid = left + (right - left) // 2 if nums[mid] > nums[-1]: left = mid + 1 else: right = mid - 1 def binarySearch(left_boundary: int, right_boundary: int, target: int): left, right = left_boundary, right_boundary while left <= right: mid = left + (right - left) // 2 if nums[mid] == target: return mid elif nums[mid] > target: right = mid - 1 else: left = mid + 1 return -1 if (answer := binarySearch(0, left-1, target)) != -1: return answer return binarySearch( left, n-1, target)
LeetCode Problem Extension Exercises
Other problem-solving notes in the same series are recommended for practice:
Search in Rotated Sorted Array: Solution Approach
The array has been rotated and is no longer ordered overall, but the key observation is that after being cut at the midpoint, at least one half of each half must be completely ordered. By first determining which half is ordered, and then checking if the target value falls within that ordered interval, we can decide which half to discard—the skeleton of the binary search is completely preserved.
Search in Rotated Sorted Array Algorithm Steps
- Let left = 0 and right = n-1, then enter the binary search loop.
- Calculate mid. If nums[mid] equals target, return mid directly.
- If nums[left] <= nums[mid] means left half ordered: if the target falls within [nums[left], nums[mid]), search to the left; otherwise, search to the right.
- Otherwise, the right half is ordered: if the target falls within (nums[mid], nums[right]], search to the right; otherwise, search to the left.
Complexity analysis of search in Rotated Sorted Array
| project | Complexity |
|---|---|
| Time complexity | O(log n) |
| Space complexity | O(1) |
Discarding half of the search space in each round results in a time complexity of O(log n). Using only a few index variables results in a space complexity of O(1). The difficulty of this problem lies not in the complexity, but in correctly writing the four branches for "determining which half is ordered".
Boundary cases and common errors in searching in Rotated Sorted Arrays
- If the value is not found, return -1, not left.
- Boundary comparison must use <= and >=, missing an equal sign will cause an error on arrays of length 1 and 2.
- If the array is not rotated at all (and is already sorted), the solution should still correctly degenerate into a general binary search.
- This problem assumes that the elements are unique; if duplicates are allowed, the case where nums[left] == nums[mid] needs to be handled separately.
Comparison of other solutions for Search in Rotated Sorted Array
| solution | time | space | illustrate |
|---|---|---|---|
| Linear scan | O(n) | O(1) | Correct, but the ordered property was not used. |
| First use binary search to find the rotation point, then use binary search to find the target. | O(log n) | O(1) | The two-part structure is logically clear, but the code is quite long. |
| Single Improved Binary Search | O(log n) | O(1) | This article adopts |
