[Java] 이진수의 0 간격

urzi·2021년 8월 25일
0

코딩테스트

목록 보기
5/20

Type

binary

내용

10진수 -> 2진수로 변경하고 0의 연속된 길이가 제일 큰 값을 구한다.

public int solution(int K) {

        int answer = 0;
        int cnt = 0;

        String str = Integer.toBinaryString(K);

        for (int i = 0; i <= str.length() - 1; i++) {
            if (str.charAt(i) == '0') {
                cnt++;
            } else {
                if (answer < cnt) {
                    answer = cnt;
                }
                cnt = 0;
            }

        }


        return answer;
    }
profile
Back-end Developer

0개의 댓글