이진 변환 반복하기

문제


문제 풀이

이진법 문자열에서 0의 개수를 세어주는 반복문과 0을 제거하는 함수, 그리고 남은 문자열 길이에 맞춰서 이진수로 변환해주는 함수를 사용을 하면 쉽게 풀 수 있는 문제이다.


나의 답안

public static int[] solution(String s) {
    int countZero = 0;
    int count = 0;
    while (!"1".equals(s)) {
        count++;
        for (char c : s.toCharArray()) {
            if (c == '0') countZero++;
        }
        s = s.replace("0", "");
        s = Integer.toBinaryString(s.length());
    }

    return new int[]{count, countZero};
}

'Programmers 문제풀이 > Lv.2' 카테고리의 다른 글

더 맵게  (0) 2023.03.27
숫자의 표현  (0) 2023.03.26
올바른 괄호  (0) 2023.03.26
최솟값 만들기  (0) 2023.03.26
JadenCase 문자열 만들기  (0) 2023.03.26