|
| 1 | + |
| 2 | +<!-- problem:start --> |
| 3 | + |
| 4 | +# [33. Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array) |
| 5 | + |
| 6 | + |
| 7 | +--- |
| 8 | +- **comments**: true |
| 9 | +- **difficulty**: Medium |
| 10 | +- **tags**: |
| 11 | + - Array |
| 12 | + - Binary Search |
| 13 | +--- |
| 14 | + |
| 15 | +## Description |
| 16 | + |
| 17 | +<!-- description:start --> |
| 18 | + |
| 19 | +<p>There is an integer array <code>nums</code> sorted in ascending order (with <strong>distinct</strong> values), which is rotated at an unknown pivot index <code>k</code> (0 <= k < nums.length) such that the resulting array is <code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]</code> (0-indexed). For example, <code>[0,1,2,4,5,6,7]</code> might be rotated at pivot index <code>3</code> and become <code>[4,5,6,7,0,1,2]</code>.</p> |
| 20 | + |
| 21 | +<p>Given the array <code>nums</code> after the rotation and an integer <code>target</code>, return the index of <code>target</code> if it is in <code>nums</code>, or <code>-1</code> if it is not in <code>nums</code>.</p> |
| 22 | + |
| 23 | +<p>You must write an algorithm with <code>O(log n)</code> runtime complexity.</p> |
| 24 | + |
| 25 | +<!-- description:end --> |
| 26 | + |
| 27 | +## Solutions |
| 28 | + |
| 29 | +<!-- solution:start --> |
| 30 | + |
| 31 | +### Solution 1: Binary Search |
| 32 | + |
| 33 | +Since the array is rotated, we can't directly apply the standard binary search. But we can still use binary search with some modifications by identifying which half of the array is properly sorted. |
| 34 | + |
| 35 | +At each step: |
| 36 | + |
| 37 | +- Check if the left half is sorted. |
| 38 | +- If it is, check whether the target is within the left half. |
| 39 | +- If not, search the right half. |
| 40 | +- Repeat similarly if the right half is sorted. |
| 41 | + |
| 42 | +This approach ensures that with each iteration, we eliminate half the array, maintaining the logarithmic complexity. |
| 43 | + |
| 44 | +Time Complexity: $O(\log n)$ |
| 45 | +Space Complexity: $O(1)$ |
| 46 | + |
| 47 | +<!-- tabs:start --> |
| 48 | + |
| 49 | +#### Python3 |
| 50 | + |
| 51 | +```python |
| 52 | +class Solution: |
| 53 | + def search(self, nums: List[int], target: int) -> int: |
| 54 | + left, right = 0, len(nums) - 1 |
| 55 | + while left <= right: |
| 56 | + mid = (left + right) // 2 |
| 57 | + if nums[mid] == target: |
| 58 | + return mid |
| 59 | + if nums[left] <= nums[mid]: |
| 60 | + if nums[left] <= target < nums[mid]: |
| 61 | + right = mid - 1 |
| 62 | + else: |
| 63 | + left = mid + 1 |
| 64 | + else: |
| 65 | + if nums[mid] < target <= nums[right]: |
| 66 | + left = mid + 1 |
| 67 | + else: |
| 68 | + right = mid - 1 |
| 69 | + return -1 |
| 70 | +``` |
| 71 | + |
| 72 | +#### Java |
| 73 | + |
| 74 | +```java |
| 75 | +class Solution { |
| 76 | + public int search(int[] nums, int target) { |
| 77 | + int l = 0, r = nums.length - 1; |
| 78 | + while (l <= r) { |
| 79 | + int mid = (l + r) / 2; |
| 80 | + if (nums[mid] == target) return mid; |
| 81 | + if (nums[l] <= nums[mid]) { |
| 82 | + if (nums[l] <= target && target < nums[mid]) { |
| 83 | + r = mid - 1; |
| 84 | + } else { |
| 85 | + l = mid + 1; |
| 86 | + } |
| 87 | + } else { |
| 88 | + if (nums[mid] < target && target <= nums[r]) { |
| 89 | + l = mid + 1; |
| 90 | + } else { |
| 91 | + r = mid - 1; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + return -1; |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +<!-- tabs:end --> |
| 101 | + |
| 102 | +<!-- solution:end --> |
| 103 | + |
| 104 | +<!-- problem:end --> |
| 105 | +``` |
0 commit comments