|
1 | 1 | ---
|
2 | 2 | id: search-in-rotated-sorted-array
|
3 | 3 | title: Search in Rotated Sorted Array (LeetCode)
|
4 |
| -difficulty: Medium |
5 | 4 | sidebar_label: 0033-SearchInRotatedSortedArray
|
6 | 5 | description: Search for a target element in a rotated sorted array with distinct values using an algorithm with O(log n) runtime complexity.
|
7 | 6 | ---
|
8 | 7 |
|
9 | 8 | ## Problem Description
|
10 | 9 |
|
11 |
| - |
12 | 10 | | Problem Statement | Solution Link | LeetCode Profile |
|
13 | 11 | | :---------------- | :------------ | :--------------- |
|
14 | 12 | | [Merge Two Sorted Lists](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/search-in-rotated-sorted-array/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) |
|
15 | 13 |
|
16 | 14 | ## Problem Description
|
17 | 15 |
|
18 |
| -There is an integer array $nums$ sorted in ascending order (with distinct values). |
| 16 | +There is an integer array nums sorted in ascending order (with distinct values). |
19 | 17 |
|
20 |
| -Prior to being passed to your function, $nums$ is possibly rotated at an unknown pivot index $k$ (0 <= 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). |
| 18 | +Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (0 <= 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). |
21 | 19 |
|
22 |
| -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$. |
| 20 | +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. |
23 | 21 |
|
24 | 22 | You must write an algorithm with $O(log n)$ runtime complexity.
|
25 | 23 |
|
26 | 24 | ### Examples
|
27 | 25 |
|
28 | 26 | #### Example 1
|
29 | 27 |
|
30 |
| -- **Input:** $nums = [4,5,6,7,0,1,2]$, $target = 0$ |
31 |
| -- **Output:** $4$ |
32 |
| -- **Explanation:** 0 is located at index 4 in the rotated sorted array $[4,5,6,7,0,1,2]$. |
| 28 | +- **Input:** nums = [4,5,6,7,0,1,2], target = 0 |
| 29 | +- **Output:** 4 |
| 30 | +- **Explanation:** 0 is located at index 4 in the rotated sorted array [4,5,6,7,0,1,2]. |
33 | 31 |
|
34 | 32 | #### Example 2
|
35 | 33 |
|
36 |
| -- **Input:** $nums = [4,5,6,7,0,1,2]$, $target = 3$ |
37 |
| -- **Output:** $-1$ |
38 |
| -- **Explanation:** 3 is not in $nums$, so return -1. |
| 34 | +- **Input:** nums = [4,5,6,7,0,1,2], target = 3 |
| 35 | +- **Output:** -1 |
| 36 | +- **Explanation:** 3 is not in nums, so return -1. |
39 | 37 |
|
40 | 38 | #### Example 3
|
41 | 39 |
|
42 |
| -- **Input:** $nums = [1]$, $target = 0$ |
43 |
| -- **Output:** $-1$ |
44 |
| -- **Explanation:** 0 is not in $nums$, so return -1. |
| 40 | +- **Input:** nums = [1], target = 0 |
| 41 | +- **Output:** -1 |
| 42 | +- **Explanation:** 0 is not in nums, so return -1. |
45 | 43 |
|
46 | 44 | ### Constraints
|
47 | 45 |
|
48 | 46 | - $v1 <= nums.length <= 5000$
|
49 | 47 | - $-10^4 <= nums[i] <= 10^4$
|
50 | 48 | - All values of $nums$ are unique.
|
51 |
| -- $nums$ is an ascending array that is possibly rotated. |
| 49 | +- nums is an ascending array that is possibly rotated. |
52 | 50 | - $-10^4 <= target <= 10^4$
|
53 | 51 |
|
54 | 52 | ### Approach
|
|
0 commit comments