akjfal

[Programmers] 이진 변환 반복하기 본문

알고리즘/programmers

[Programmers] 이진 변환 반복하기

akjfal 2021. 6. 19. 13:35
class Solution {
    int[] answer;
    public int[] solution(String s) {
        answer = new int[2];
        String beforeS = s;
        while(true){
            String changeS = changeBit(beforeS);
            answer[0]++;
            if(changeS.equals("1")){
                break;
            }else{
                beforeS = changeS;
            }
        }
        return answer;
    }
    
    public String changeBit(String s){
        int c;
        String changeS = "";
        String removeZero = s.replaceAll("0", "");
        int zeroLength = removeZero.length();
        answer[1] += s.length() - zeroLength;
        c = removeZero.length();
        while(c > 0){
            changeS = c%2 + changeS;
            c = c/2;
        }
        return changeS;
    }
}

.

 

Comments