-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathBinaryGap_2.cs
More file actions
39 lines (30 loc) · 797 Bytes
/
BinaryGap_2.cs
File metadata and controls
39 lines (30 loc) · 797 Bytes
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
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution
{
public int solution(int N)
{
// write your code in C# 6.0 with .NET 4.5 (Mono)
int i = N;
int max_gap = 0;
int current_gap = -1;
while (i > 0)
{
if (i % 2 != 0)
{
max_gap = Math.Max(max_gap,current_gap);
current_gap = 0;
}
else if(current_gap >= 0)
{
current_gap++;
}
i >>= 1;
// i /= 2;
}
return max_gap;
}
}