-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSearch in Rotated Sorted Array.cpp
42 lines (38 loc) · 1.17 KB
/
Search in Rotated Sorted Array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
public:
int binarySearch(vector<int> nums, int low, int high, int target)
{
while(low <= high)
{
int mid = (low + high) / 2;
if(nums[mid] == target)
return mid;
else if(nums[mid] < target)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int search(vector<int>& nums, int target) {
if(nums.size() == 1)
return (nums[0] == target) ? 0 : -1;
int low = 0, high = nums.size() - 1, mid, pivot, n = nums.size() - 1;
while(low <= high)
{
int mid = (low + high) / 2;
if(nums[mid] > nums[mid + 1])
{
pivot = mid;
break;
}
else if(nums[mid] < nums[0])
high = mid - 1;
else if(nums[mid] > nums[n])
low = mid + 1;
else
return binarySearch(nums, low, high, target);
}
return (target < nums[0]) ? binarySearch(nums, pivot + 1, n, target) : binarySearch(nums, 0, pivot, target);
}
};