#33

Search in Rotated Sorted Array

specialist · 955 · lc medium +32 · verified · 44.2% accepted · 29,786 likes · top 26%

play →

Description

An ascending-sorted array nums of distinct integers was rotated at an unknown pivot. Search for target in the rotated array and return its index, or -1 if absent. The solution must run in O(log n) time.

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

Code

1
2
3