-Code
class Solution {
public int solution(int n) {
int one_cnt = getOneCnt(Integer.toString(n, 2));
int num = n;
while (true) {
num++;
int next_one_cnt = getOneCnt(Integer.toString(num, 2));
if (one_cnt == next_one_cnt) {
break;
}
}
return num;
}
public static int getOneCnt(String bin) {
int answer = 0;
for (char c : bin.toCharArray()) {
if (c == '1') answer++;
}
return answer;
}
}
'Beakjoon&프로그래머스 > Java' 카테고리의 다른 글
[프로그래머스/Java] 짝지어 제거하기 (0) | 2025.02.28 |
---|---|
[프로그래머스/Java] 피보나치 수 (0) | 2025.02.28 |
[프로그래머스/Java] 숫자의 표현 (0) | 2025.02.28 |
[프로그래머스/Java] 이진 변환 반복하기 (0) | 2025.02.28 |
[프로그래머스/Java] 올바른 괄호 (0) | 2025.02.28 |