33. Search in Rotated Sorted Array
√ Accepted
√ 196/196 cases passed (4 ms)
√ Your runtime beats 100 % of cpp submissions
√ Your memory usage beats 98.58 % of cpp submissions (8.9 MB)
class Solution {
public:
int search(vector<int>& nums, int target) {
int l = 0;
int r = nums.size() - 1;
while(l <= r) {
int mid = l + (r - l) / 2;
if(nums[mid] == target) {
return mid;
}
if(nums[mid] < target) {
if (nums[r] < target && nums[mid] < nums[r])
{
r = mid - 1;
}
else
{
l = mid + 1;
}
}
else
{
if(nums[l] > target && nums[l] <= nums[mid])
{
l = mid + 1;
} else
{
r = mid - 1;
}
}
}
return -1;
}
};