33_Search in Rotated Sorted Array
Last updated
Last updated
[Medium][Array, Binary Search]
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e.,0 1 2 4 5 6 7
might become4 5 6 7 0 1 2
). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array.
Example 1:
Example 2:
Idea: Binary Search
Since the array is rotated sorted, there must be one side that is in ascending order. Find that side and check if the target is in this side. If yes, throw away the other side. If no, throw away this side.
The most important thing I learn from this problem is don't make the conditions for binary search too complicated. We don't need to specify each nested conditions. We can use one simple condition for one side and else for the other side.
Time Complexity:
Space Complexity: