
public class 다음큰숫자 {
public static int solution(int n) {
/*
while( true ) {
if( n의 2진수 1의 갯수 == ++n의 2진수 1의 갯수 ) {
break
}
}
return n
*/
int cnt = Integer.bitCount(n); // n의 이진수에서 1의 개수 계산 ( O(1) )
while (true) { // O(n)
if (cnt == Integer.bitCount(++n)) {
break;
}
}
return n;
}
public static void main(String[] args) {
System.out.println(solution(78));
System.out.println(solution(15));
}
}
Integer.bitCount(n)
- 주어진 정수 n의 이진수 표현에서 1의 개수를 계산한다
반응형
'IT > Algorithm | Coding Test' 카테고리의 다른 글
| [프로그래머스 138476] [Java] 귤 고르기 (2) | 2025.03.17 |
|---|---|
| [프로그래머스 12945] [Java] 피보나치 수 (0) | 2025.03.11 |
| [프로그래머스 70129] [Java] 이진 변환 반복하기 (0) | 2025.02.27 |
| [프로그래머스 12941] [Java] 최솟값 만들기 (0) | 2025.02.25 |
| [프로그래머스 12951] [Java] JadenCase 문자열 만들기 (0) | 2025.02.25 |