Binary Gap LeetCode solution in C++

1
class Solution {
2
public:
3
int binaryGap(int n) {
4
int res = 0;
5
int s = 0, i = 0;
6
while (n) {
7
if (n & 1) {
8
s = i;
9
break;
10
}
11
i++;
12
n = n >> 1;
13
}
14
while (n) {
15
if (n & 1) {
16
res = max(res, (i - s));
17
s = i;
18
}
19
i++;
20
n = n >> 1;
21
}
22
return res;
23
}
24
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0