forked from deepaktalwardt/interview-prep-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path33-search-in-rotated-sorted-array.cpp
61 lines (58 loc) · 2.04 KB
/
33-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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Iterative solution
class Solution {
public:
int search(vector<int>& nums, int target) {
int leftIdx = 0;
int rightIdx = nums.size() - 1;
while (leftIdx <= rightIdx) {
int midIdx = (leftIdx + rightIdx) / 2;
if (nums[midIdx] == target) {
return midIdx;
}
if (nums[leftIdx] <= nums[midIdx]) { // left is sorted
if (target >= nums[leftIdx] && target < nums[midIdx]) {
rightIdx = midIdx - 1;
} else {
leftIdx = midIdx + 1;
}
} else { // right is sorted
if (target > nums[midIdx] && target <= nums[rightIdx]) {
leftIdx = midIdx + 1;
} else {
rightIdx = midIdx - 1;
}
}
}
return -1;
}
};
// Recursive solution
class Solution {
public:
int shiftedBinarySearch(vector<int>&nums, int target, int leftIdx, int rightIdx) {
if (leftIdx > rightIdx) return -1;
int mid = (leftIdx + rightIdx) / 2;
int leftNum = nums[leftIdx];
int rightNum = nums[rightIdx];
int midNum = nums[mid];
if (midNum == target) {
return mid;
}
if (leftNum <= midNum) { // left is sorted
if (target >= leftNum && target < midNum) {
return shiftedBinarySearch(nums, target, leftIdx, mid - 1);
} else {
return shiftedBinarySearch(nums, target, mid + 1, rightIdx);
}
} else { // right is sorted
if (target > midNum && target <= rightNum) {
return shiftedBinarySearch(nums, target, mid + 1, rightIdx);
} else {
return shiftedBinarySearch(nums, target, leftIdx, mid - 1);
}
}
}
int search(vector<int>& nums, int target) {
return shiftedBinarySearch(nums, target, 0, nums.size() - 1);
}
};