[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] LV.2 ์ด์ง„๋ณ€ํ™˜๋ฐ˜๋ณตํ•˜๊ธฐ (์ž๋ฐ” java)

 

1. ๋ฌธ์ œ : https://school.programmers.co.kr/learn/courses/30/lessons/70129

 

2. ํ’€์ด

๋ฌธ์ž์—ด ํ•˜๋‚˜์”ฉ ์ฝ์œผ๋ฉด์„œ 0์„ ํ™•์ธํ•˜๋ฉด์„œ ํ’€์—ˆ์ง€๋งŒ

replaceAll ํ†ตํ•ด์„œ 0์„ ์ œ๊ฑฐํ•˜๊ณ  ๊ธธ์ด ๋น„๊ตํ•˜๋Š” ๊ฒŒ ๋” ํšจ์œจ์ ์ธ ๊ฒƒ ๊ฐ™๋‹ค.

 

3. ์ฝ”๋“œ

๋‚˜์˜ ํ’€์ด

class Solution {
    public int[] solution(String s) {
        int answer_cnt = 0;
        int answer_zero = 0;
        int x_len = s.length();
        
        while(x_len>1){
            String temp = "";
            for(int i=0;i<s.length();i++){
                char schar = s.charAt(i);
                if(schar-'0'!= 0) temp += schar;
                else answer_zero ++;
            }
            answer_cnt++;
            
            x_len = temp.length();
            s = Integer.toBinaryString(x_len);
        }
        int[] answer = {answer_cnt,answer_zero};
        return answer;
    }
}

์ฐธ๊ณ ์ฝ”๋“œ

class Solution {
    public int[] solution(String s) {
        int[] answer = new int[2];
        int temp;
        while( !s.equals("1") ) {
            answer[1] += s.length();
            s = s.replaceAll("0", "");
            temp = s.length();
            s = Integer.toBinaryString(temp);
            //System.out.println("s : " + s ); 
            answer[0]++;
            answer[1] -= temp;
        }
        return answer;  
    }
}

 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ