-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLesson1_Iterations_BinaryGap.cs
61 lines (46 loc) · 1.46 KB
/
Lesson1_Iterations_BinaryGap.cs
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
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");
namespace Lesson1_Iterations_BinaryGap
{
class Solution {
public void Test()
{
if(true)
{
var Solution = new Lesson1_Iterations_BinaryGap.Solution();
var x=Solution.solution(8);
Console.WriteLine($"solution.solution == {x} ");
x=Solution.solution(1024);
Console.WriteLine($"solution.solution == {x} ");
x=Solution.solution(1041);
Console.WriteLine($"solution.solution == {x} ");
}
}
public int solution(int N) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
Console.WriteLine($"inside solution.solution input param {N}");
var b = Convert.ToString(N, 2);
Console.WriteLine($"inside solution.solution convert to bool {b}");
// take string convert to array
var a = b.ToCharArray();
// setup
var zeros = 0;
var maxZeros = 0;
// iterate over a
foreach(char c in a)
{
if( c == '1')
{
if ( maxZeros < zeros ) maxZeros = zeros;
zeros = 0;
continue;
}
zeros++;
}
return maxZeros;
}
}
}