Leetcode Problem 33. Search in Rotated Sorted Array

33. Search in Rotated Sorted Array

Leetcode Solutions

Approach: One Binary Search

  1. Initialize left to 0 and right to n - 1.
  2. While left <= right: a. Calculate mid as the midpoint between left and right. b. If nums[mid] is equal to target, return mid. c. If nums[mid] is greater than or equal to nums[left], the left half is sorted. i. If target is between nums[left] and nums[mid], set right to mid - 1. ii. Else, set left to mid + 1. d. Else, the right half is sorted. i. If target is between nums[mid] and nums[right], set left to mid + 1. ii. Else, set right to mid - 1.
  3. If the target is not found, return -1.
UML Thumbnail

Approach: Find Pivot Index + Binary Search

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Code Diffs Compare
Full Screen
Copy Answer Code
Loading...