[Java] Programmers - Next Big Number
This post uses Java Integer.bitCount() to find the next larger number with the same number of 1 bits, and notes the efficiency limits of a brute-force approach.
한국어 원문은 여기에서 볼 수 있습니다.
[Java] Programmers - Next Big Number
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public int solution(int n) {
int target = Integer.bitCount(n);
while(target != Integer.bitCount(++n)) {
}
return n;
}
}
- I continued to solve the problem using different solving methods, but it was difficult to pass the accuracy test but not the efficiency test.
Integer.bitCount(n)
- A function that immediately counts the number of 1s when the number is converted to binary in Java.