
728x90
import java.util.*;
class Solution {
public int[] solution(String s) {
int[] answer = new int[2];
//이진수가 1이 될때까지 반복
while(!s.equals("1")){
List<String> split = List.of(s.split(""));
//0을 제거한 길이 == s에 있는 1의 개수
int noZeroLength = 0;
for(String number : split){
//0일 경우 == 0을 제거한 횟수 +1
if(number.equals("0")){
answer[1] ++;
} else if(number.equals("1")){
noZeroLength +=1;
}
}
//0을 제거한 길이를 이진수로 변환
s = Integer.toBinaryString(noZeroLength);
//위의 변환과정 끝나면 +1
answer[0] ++;
}
return answer;
}
}
728x90
반응형
'알고리즘' 카테고리의 다른 글
이중우선순위큐(Lv.3)[프로그래머스] (0) | 2022.09.20 |
---|---|
최솟값 만들기(Lv.2)[프로그래머스] (0) | 2022.09.19 |
점프와 순간 이동(Lv.2)[프로그래머스] (0) | 2022.09.16 |
올바른 괄호(Lv.2)[프로그래머스] (0) | 2022.09.16 |
JadenCase 문자열 만들기(Lv.2)[프로그래머스] (0) | 2022.09.15 |