프로그래밍/알고리즘

[Codility] Binary Gap 알고리즘

낭만가을 2018. 4. 14. 02:50

나의 sloution 은 아래와 같다.


class Solution {

public int solution(int N) { // write your code in Java SE 8 int binaryGap =0; int highestGap =0; String binary = Integer.toBinaryString(N); boolean meetOne = false; for(int i=0 ;i< binary.length();i++){ char index = binary.charAt(i); // System.out.println("#### index = "+index); if(index == '1'){ meetOne = true; if(binaryGap > highestGap){ highestGap = binaryGap; } binaryGap = 0; }else if(index == '0'){ if(meetOne){ binaryGap++; } } } return highestGap; } }


100% 이지만 코드는 좀 더 보완이 필요해 보인다.