Created
January 21, 2018 19:06
-
-
Save conr/78751c9e465c19335c0a182c718ecd25 to your computer and use it in GitHub Desktop.
Search in rotated sorted array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def search(nums, target) | |
if nums.length == 0 | |
return -1 | |
end | |
low, high = 0, nums.length-1 | |
while low <= high | |
mid = (high+low)/2 | |
if nums[mid] == target | |
return mid | |
end | |
if nums[low] <= nums[mid] | |
if nums[low] <= target && target < nums[mid] | |
high = mid - 1 | |
else | |
low = mid + 1 | |
end | |
else | |
if nums[mid] < target && target <= nums[high] | |
low = mid + 1 | |
else | |
high = mid - 1 | |
end | |
end | |
end | |
return -1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment