bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 81. Search in Rotated Sorted Array II

81. Search in Rotated Sorted Array II

Leetcode Solutions

Binary Search with Duplicate Elements

  1. Initialize start to 0 and end to len(nums) - 1.
  2. While start is less than or equal to end: a. Calculate mid as the midpoint between start and end. b. If nums[mid] equals target, return true. c. If nums[start] is less than nums[mid], the left half is sorted: i. If target is between nums[start] and nums[mid], search the left half by setting end to mid - 1. ii. Otherwise, search the right half by setting start to mid + 1. d. If nums[start] is greater than nums[mid], the right half is sorted: i. If target is between nums[mid] and nums[end], search the right half by setting start to mid + 1. ii. Otherwise, search the left half by setting end to mid - 1. e. If nums[start], nums[mid], and nums[end] are equal, we cannot determine the correct half, so we increment start and decrement end.
  3. If the loop ends without finding the target, return false.
UML Thumbnail

Linear Search

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...