-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsolution033.cpp
More file actions
60 lines (53 loc) · 827 Bytes
/
solution033.cpp
File metadata and controls
60 lines (53 loc) · 827 Bytes
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
/**
* Search in rotate array.
* Binary search.
*
* cpselvis ([email protected])
*/
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
int search(vector<int>& nums, int target) {
int l = 0, r = nums.size() - 1;
while (l <= r)
{
int m = (l + r) >> 1;
if (nums[m] == target)
{
return m;
}
if (nums[l] <= nums[m])
{
if (target >= nums[l] && target < nums[m])
{
r = m - 1;
}
else
{
l = m + 1;
}
}
else
{
if (target > nums[m] && target <= nums[r])
{
l = m + 1;
}
else
{
r = m - 1;
}
}
}
return -1;
}
};
int main(int argc, char **argv)
{
Solution s;
int arr[7] = {0, 1, 2, 4, 5, 6, 7};
vector<int> vec(arr + 0, arr + 7);
cout << s.search(vec, 4) << endl;
}