-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch-in-rotated-sorted-array.cc
70 lines (65 loc) · 2.41 KB
/
search-in-rotated-sorted-array.cc
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
62
63
64
65
66
67
68
69
70
#include "leetcode.h"
using namespace std;
class Solution {
public:
int search(vector<int> &numbers, const int target) {
if (numbers.empty()) return -1;
auto index = -1;
search(numbers, target, 0, numbers.size() - 1, index);
return index;
}
void search(vector<int> &numbers, int target, int start, int end, int &out) {
switch (end - start) {
case 0:
if (numbers[start] == target) out = start;
return;
case 1:
if (numbers[start] == target)
out = start;
else if (numbers[end] == target)
out = end;
return;
default:
const auto mid = (end + start) / 2;
if (numbers[mid] == target) {
out = mid;
} else if (numbers[start] < numbers[mid]) {
if (target >= numbers[start] && target <= numbers[mid])
binary_search(numbers, target, start, mid - 1, out);
else
search(numbers, target, mid + 1, end, out);
} else if (numbers[mid] < numbers[end]) {
if (target >= numbers[mid] && target <= numbers[end])
binary_search(numbers, target, mid + 1, end, out);
else
search(numbers, target, start, mid - 1, out);
}
}
}
void binary_search(vector<int> &numbers, const int target, const int start, const int end, int &out) {
switch (end - start) {
case 0:
if (numbers[start] == target) out = start;
return;
case 1:
if (numbers[start] == target)
out = start;
else if (numbers[end] == target)
out = end;
return;
default:
const auto mid = (start + end) / 2;
if (numbers[mid] == target)
out = mid;
else if (numbers[mid] < target)
binary_search(numbers, target, mid + 1, end, out);
else
binary_search(numbers, target, start, mid - 1, out);
}
}
};
int main(int argc, char const *argv[]) {
Solution solution;
vector<int> vec{4, 5, 6, 7, 0, 1, 2};
cout << solution.search(vec, 0);
}