Practice/Meta/Leetcode 33. Search in Rotated Sorted Array
CodingMust
You are given an integer array that was originally sorted in ascending order with distinct values. However, this array has been rotated at some unknown pivot index. For example, the array [0, 1, 2, 4, 5, 6, 7] might become [4, 5, 6, 7, 0, 1, 2] if rotated at pivot index 3.
Given this rotated array and a target integer value, write a function that returns the index position of the target value. If the target does not exist in the array, return -1.
Your solution must achieve O(log n) time complexity.
Example 1:
Input: nums = [4, 5, 6, 7, 0, 1, 2], target = 0 Output: 4 Explanation: The target value 0 is located at index 4
Example 2:
Input: nums = [4, 5, 6, 7, 0, 1, 2], target = 3 Output: -1 Explanation: The value 3 does not exist in the array
Example 3:
Input: nums = [1], target = 0 Output: -1 Explanation: Single element array does not contain the target
Example 4:
Input: nums = [5, 1, 3], target = 3 Output: 2 Explanation: The target value 3 is at index 2