Skip to content

Commit ea3e668

Browse files
committed
binary gap
1 parent 7fa0834 commit ea3e668

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

BinaryGap.cs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
namespace Test
3+
{
4+
public class BinaryGap
5+
{
6+
static void Main(string[] args)
7+
{
8+
Console.WriteLine(Solution(2147483647));
9+
Console.WriteLine(Solution(529));
10+
Console.WriteLine(Solution(1041));
11+
Console.WriteLine(Solution(1));
12+
Console.WriteLine(Solution(3));
13+
Console.WriteLine(Solution(5));
14+
Console.WriteLine(Solution(32));
15+
}
16+
17+
static int Solution(int N)
18+
{
19+
int Longest = 0, ZeroCtr = 0;
20+
21+
foreach (char c in Convert.ToString(N, 2))
22+
{
23+
if (c == '0')
24+
ZeroCtr++;
25+
else
26+
{
27+
if (ZeroCtr > Longest)
28+
Longest = ZeroCtr;
29+
ZeroCtr = 0;
30+
}
31+
}
32+
33+
return Longest;
34+
}
35+
36+
}
37+
}

0 commit comments

Comments
 (0)