Scroll indicator done
728x90

https://school.programmers.co.kr/learn/courses/30/lessons/70129

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


조건
  1. x의 모든 0을 제거합니다.
  2. x의 길이를 c라고 하면, x를 "c를 2진법으로 표현한 문자열"로 바꿉니다.

import java.util.Arrays;

public class Solution70129 {
    public int[] solution(String s) {
        int cnt = 0, zero = 0;  // 변환 횟수, 제거한 0 개수
        while(!s.equals("1")){  // s 가 "1"이 될 때까지 
            cnt += 1;
            zero += s.length() - s.replace("0", "").length();  // s의 전체 길이 - 0을 제거한 s 길이 (제거한 0의 개수 카운트)
            s = s.replace("0", "");  //  s를 0을 제거한 s로 변환
            s = Integer.toBinaryString(s.length());  //  s의 길이를 이진 변환
        }
        return new int[]{cnt, zero}; 
    }

    public static void main(String[] args) {
        Solution70129 solution70129 = new Solution70129();
        int[] arr = solution70129.solution("01110");
        System.out.println(Arrays.toString(arr));
    }
}
728x90