Algorithm
Problem Name: 33. Search in Rotated Sorted Array
Problem Link: https://leetcode.com/problems/search-in-rotated-sorted-array/
There is an integer array nums
sorted in ascending order (with distinct values).
Prior to being passed to your function, nums
is possibly rotated at an unknown pivot index k
(1 <= k < nums.length
) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]
(0-indexed). For example, [0,1,2,4,5,6,7]
might be rotated at pivot index 3
and become [4,5,6,7,0,1,2]
.
Given the array nums
after the possible rotation and an integer target
, return the index of target
if it is in nums
, or -1
if it is not in nums
.
You must write an algorithm with O(log n)
runtime complexity.
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
Constraints:
1 <= nums.length <= 5000
-104 <= nums[i] <= 104
- All values of
nums
are unique. nums
is an ascending array that is possibly rotated.-104 <= target <= 104
Code Examples
#1 Code Example with C Programming
Code -
C Programming
int search(int* nums, int numsSize, int target) {
int start, end, mid;
start = 0; end = numsSize - 1;
while (start < = end) {
mid = start + (end - start) / 2;
if (nums[mid] == target) return mid;
if (nums[start] < = nums[mid]) { // first half are sorted
if (target > nums[mid] || target < nums[start]) {
start = mid + 1;
} else {
end = mid - 1;
}
} else { // second half are sorted
if (target < nums[mid] || target > nums[end]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
}
return -1;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
class Solution {
public:
int search(vector<int>& nums, int target) {
int n = nums.size();
if(n == 0) return -1;
int lo = 0, hi = n - 1;
int mid = (lo + hi) / 2;
while(lo < hi>{
if(nums[mid] > nums[hi]) lo = mid + 1;
else hi = mid;
mid = (lo + hi) / 2;
}
int pos = (target >= nums[0] && lo != 0) ? lower_bound(nums.begin(), nums.begin() + lo, target) - nums.begin()
: lower_bound(nums.begin() + lo, nums.end(), target) - nums.begin();
return nums[pos] == target ? pos : -1;
}
};
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Java Programming
Code -
Java Programming
class Solution {
public int search(int[] nums, int target) {
int start = 0;
int end = nums.length - 1;
while (start < = end) {
int mid = (start + end) / 2;
if (nums[mid] == target) {
return mid;
}
if (nums[mid] >= nums[start]) {
if (nums[start] < = target && nums[mid] > target) {
end = mid - 1;
}
else {
start = mid + 1;
}
}
else {
if (nums[end] >= target && nums[mid] < target) {
start = mid + 1;
}
else {
end = mid - 1;
}
}
}
return -1;
}
}
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Javascript Programming
Code -
Javascript Programming
const search = function(nums, target) {
const len = nums.length
let r = false
let ridx = 0
if(len === 0) return -1
if(nums[0] === target) return 0
for(let i = 1; i < len; i++) {
if(nums[i] === target) return i
if(nums[i] < nums[i - 1]) {
r = true
ridx = i
break
}
}
if(r === true> {
for(let i = len - 1; i >= ridx; i--) {
if(nums[i] === target) return i
}
}
return -1
};
Copy The Code &
Try With Live Editor
Input
Output
#5 Code Example with Python Programming
Code -
Python Programming
class Solution:
def search(self, nums, target):
l, r = 0, len(nums) - 1
while l <= r:
mid = (l + r) // 2
if nums[mid] == target:
return mid
elif sum((target < nums[l], nums[l] <= nums[mid], nums[mid] < target)) == 2:
l = mid + 1
else:
r = mid - 1
return -1
Copy The Code &
Try With Live Editor
Input
Output
#6 Code Example with C# Programming
Code -
C# Programming
namespace LeetCode
{
public class _033_SearchInRotatedSortedArray
{
public int Search(int[] nums, int target)
{
int lo = 0, hi = nums.Length - 1;
int mid, loValue, hiValue, midValue;
while (lo < = hi)
{
loValue = nums[lo];
hiValue = nums[hi];
if (loValue < = hiValue && (target < loValue || target > hiValue))
{
return -1;
}
mid = lo + (hi - lo) / 2;
midValue = nums[mid];
if (target == midValue) { return mid; }
if (loValue < = midValue)
{
if (loValue <= target && target < midValue)
{
hi = mid - 1;
}
else
{
lo = mid + 1;
}
}
else
{
if (target < = hiValue && midValue < target)
{
lo = mid + 1;
}
else
{
hi = mid - 1;
}
}
}
return -1;
}
}
}
Copy The Code &
Try With Live Editor
Input
Output