-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path53-timesnum.cpp
68 lines (55 loc) · 1.06 KB
/
53-timesnum.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
#include "stdafx.h"
#include<stdio.h>
#include"string"
#include "vector"
#include<algorithm>
using namespace std;
#include "stdafx.h"
#include<stdio.h>
#include"string"
#include "vector"
#include<algorithm>
using namespace std;
int times(vector<int> input,int target)
{
int index = input.size()/2, left = 0, right = 0;
int i = 0, j = input.size()-1;
while (input[index] != target)
{
if (input[index] < target)
index = (index + input.size()) / 2;
else
index = index / 2;
}
left = right = index;
if (input[i] == target)
left = i;
if (input[j] == target)
right = j;
while (input[i]!=target&&input[left] == target )
{
if (input[(i + left) / 2] == target)
left = (i + left) / 2;
else
i = (i + left) / 2;
if (i + 1 == left)
break;
}
while (input[j] != target && input[right] == target)
{
if (input[(j + right) / 2] == target)
right = (j + right) / 2;
else
j = (j + right) / 2;
if (right + 1 == j)
break;
}
return right - left + 1;
}
int main()
{
vector<int> a = { 1,2,3,3,3,3,4,5 };
int b = 3;
times(a,b);
return 0;
}