Skip to content

Commit 68bc0f0

Browse files
authored
[20251020] PGM / LV2 / 이진 변환 반복하기 / 이인희
1 parent f14ed8a commit 68bc0f0

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
```java
2+
import java.util.*;
3+
4+
class Solution {
5+
public int[] solution(String s) {
6+
int[] answer = new int[]{0, 0};
7+
8+
while(!"1".equals(s)){
9+
// 1개수 확인
10+
int oneCount = 0;
11+
for(int i = 0; i<s.length(); i++){
12+
if(s.charAt(i) == '1')
13+
oneCount++;
14+
}
15+
answer[1] += s.length() - oneCount;
16+
// 이진수 s에 대입
17+
StringBuilder sb = new StringBuilder();
18+
while(oneCount>1){
19+
sb.append(Integer.toString(oneCount % 2));
20+
oneCount /= 2;
21+
}
22+
sb.append(Integer.toString(oneCount));
23+
sb.reverse();
24+
s = sb.toString();
25+
answer[0]++;
26+
}
27+
28+
return answer;
29+
}
30+
}
31+
```

0 commit comments

Comments
 (0)