-
Notifications
You must be signed in to change notification settings - Fork 1
/
binaryGap.cpp
50 lines (45 loc) · 1.07 KB
/
binaryGap.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
#include <math.h>
#include <vector>
#include <stack>
#include <deque>
vector<int> to_binary( int x ) {
long int twoRaised = 0;
int power = 31;
vector<int> in_binary;
while( power >= 0 ) {
twoRaised = pow( 2, power );
if( x - twoRaised >= 0 ) {
in_binary.push_back( 1 );
x -= twoRaised;
} else {
in_binary.push_back( 0 );
}
--power;
}
return in_binary;
}
int solution(int N) {
std::stack<int> counter;
vector<int> in_binary = to_binary( N );
for( int i : in_binary ) {
counter.push( i );
}
int maxCount = 0;
int tmpCount = 0;
bool headZero = true;
while( !counter.empty() ) {
int tmp = counter.top();
counter.pop();
if( tmp == 1 && !headZero) {
if( tmpCount > maxCount )
maxCount = tmpCount;
tmpCount = 0;
} else if ( tmp == 1 && headZero ) {
tmpCount = 0;
headZero = false;
} else {
++tmpCount;
}
}
return maxCount;
}