-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode081.cpp
75 lines (64 loc) · 1.41 KB
/
leetcode081.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
67
68
69
70
71
72
73
74
75
/*************************************************************************
> File Name: leetcode081.cpp
> Author:
> Mail:
> Created Time: Wed 24 Aug 2016 12:17:31 AM PDT
************************************************************************/
#include<iostream>
#include<vector>
using namespace std;
bool search(vector<int>& nums, int target)
{
int left = 0 ;
int right = nums.size() - 1;
int mid = -1;
while(left <= right)
{
mid = (left + right) / 2;
if(nums[mid] == target)
{
return true;
}
if(nums[mid] > nums[left])
{
if(nums[left] <= target && target < nums[mid])
{
right = mid;
}
else
{
left = mid + 1;
}
}
else if(nums[mid] < nums[left])
{
if(nums[mid] < target && target <= nums[right])
{
left = mid + 1;
}
else
{
right = mid;
}
}
else
{
left++;
}
}
return false;
}
int main()
{
vector<int> nums;
nums.push_back(4);
nums.push_back(4);
nums.push_back(5);
nums.push_back(6);
nums.push_back(7);
nums.push_back(0);
nums.push_back(1);
nums.push_back(2);
cout << search(nums,3) << endl;
cout << search(nums,4) << endl;
}