Deep_Dev
article thumbnail

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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 


๋ชจ๋“  0 ์ œ๊ฑฐ ํ›„ -> ์ œ๊ฑฐํ•œ๋งŒํผ ์นด์šดํŠธ 

์ œ๊ฑฐ ๋œ ๋ฌธ์ž์—ด ๊ธธ์ด๋ฅผ ๋‹ค์‹œ 2์ง„์ˆ˜๋กœ -> ๋ณ€ํ™˜ํšŸ์ˆ˜ ์นด์šดํŠธ

 

์ด ๊ณผ์ •์„ ๋ฌธ์ž์—ด s์˜ ๊ธธ์ด๊ฐ€ 2๊นŒ์ง€ ( s.length() > 1 ) ๋ฐ˜๋ณตํ•˜๋ฉด ๋œ๋‹ค.

 

class Solution {
    public int[] solution(String s) {
        int[] answer = new int[2];

        int cnt = 0;
        int zeroCount = 0;

        while(s.length()>1){
            int s_length = s.length();
            s=s.replaceAll("0","");
            zeroCount += s_length-s.length();

            String x = Integer.toBinaryString(s.length());
            cnt++;
            s=x;
        }

        answer[0]=cnt;
        answer[1]=zeroCount;
        return answer;
    }
}