-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
48 lines (43 loc) · 1.21 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package programmers.level_2.next_number_12911;
public class Solution {
/**
* 나의 풀이법과 동일한 방식이지만 bitCount 메서드를 이용하여 간결하게 풀이하였다.
*/
public int solution(int n) {
int count = Integer.bitCount(n);
while (true) {
n++;
int myCount = Integer.bitCount(n);
if (count == myCount) {
return n;
}
}
}
public int solution2(int n) {
String str = Integer.toBinaryString(n);
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '1') {
count++;
}
}
while (true) {
int myCount = 0;
n++;
str = Integer.toBinaryString(n);
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '1') {
myCount++;
}
}
if (count == myCount) {
return n;
}
}
}
public static void main(String[] args) {
Solution solution = new Solution();
int n = 15;
System.out.println(solution.solution(n));
}
}