
내가 작성한 코드
class Solution {
public int[] solution(String s) {
int[] answer = new int[2];
int count = 0;
int zeroCount = 0;
while (!s.equals("1")) {
int oneCount = 0;
count++;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '1') {
oneCount++;
} else {
zeroCount++;
}
}
s = Integer.toBinaryString(oneCount);
}
answer[0] = count;
answer[1] = zeroCount;
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;
}
}
Share article