
answer는 {0, 0}의 int 배열로 초기화한다.
s가 "1"이 될 때까지 반복문을 수행한다.
1. 반복문을 들어오면 이진 변환을 한 번 실행하는 것이기 때문에 answer[0]에 1을 더한다.
2. s의 0의 개수를 센다. string에서 특정 문자의 개수를 세기 위해서 chars() 메서드를 이용한다.
2. 1번에서 구한 0의 개수는 answer[1]에 더해준다.
3. s의 길이에서 1번에서 구한 0의 개수를 뺀 수를 이진 변환을 해준다.
import java.util.*;
import java.util.stream.Collectors;
class Solution {
public int[] solution(String s) {
int[] answer = {0, 0};
while(!s.equals("1")) {
answer[0] += 1;
long cnt = s.chars().filter(ch -> ch == '0').count();
answer[1] += (int) cnt;
s = toBinary(s.length() - (int)cnt);
}
return answer;
}
private String toBinary (int num) {
List<Integer> binaryList = new ArrayList<Integer>();
while (num > 0) {
binaryList.add(num%2);
num /= 2;
}
Collections.reverse(binaryList);
String result = binaryList.stream()
.map(String::valueOf)
.collect(Collectors.joining());
return result;
}
}

다른 사람의 풀이를 보면서 숫자를 이진수로 변환해주는 메서드가 존재한다는 것을 알게 되었다.
이진 변환을 이번 문제를 통해 연습했다고 생각하고 다음에는 toBinaryString()을 이용해야겟다.