/
33-search-in-rotated-sorted-array.cpp
66 lines (55 loc) · 1.34 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
62
63
64
65
66
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int search(vector<int>& nums, int target) {
return dosearch(nums, target, 0, nums.size() - 1);
}
int dosearch(vector<int>& nums, int target, int l, int r){
if(l < r){
int m = (l + r) / 2;
if(nums[m] == target)
return m;
if(nums[l] <= nums[m]){
}else{
}
}
return -1;
}
};
void flip(vector<int>& a, int start, int end){
while(start < end){
int tmp = a[end];
a[end] = a[start];
a[start] = tmp;
start ++;
end --;
}
}
int main(){
//int myint[] = {0, 1, 2, 3, 5, 7, 8, 10};
int myint[] = {4,5,6,7,8,1,2,3};
//int myint[] = {0, 1, 1, 2, 3, 5, 5, 7, 8, 10};
vector<int> a(myint, myint + sizeof(myint)/sizeof(int));
int n = a.size();
cout << "Total: " << n << endl;
cout << "Input s:" << endl;
int s;
cin >> s;
s = s % n;
if(s > 0){
flip(a, 0, n - 1);
flip(a, 0, n-s-1);
flip(a, n-s, n-1);
}
for(int i=0; i<a.size(); i++){
cout << a[i] << " ";
}
cout << endl;
cout << "Input target:" << endl;
int target;
cin >> target;
Solution solution;
cout << solution.search(a, target) << endl;
}