Lesson1 - BinaryGap

作成日:2018/2/18
BinaryGap coding task - Learn to Code - Codility

// you can also use imports, for example:
// import java.util.*;

// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");

class Solution {
    public static int solution(int N) {

        String[] bin = Integer.toBinaryString(N).split("");
        int max = 0;
        int leng = 0;

        for(int i = 0; i < bin.length; i++){
          switch (bin[i]) {
            case "1" :
              if( max < leng ){ max = leng; }
              leng = 0;
              break;

            default : leng++;
          }
        }
        return max;
    }
}