Skip to content

Commit 496a1f2

Browse files
authored
Merge pull request #1810 from AlgorithmWithGod/ksinji
[20260121] PGM / LV2 / 다음 큰 숫자 / 강신지
2 parents 5070481 + 7a70926 commit 496a1f2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
```java
2+
class Solution {
3+
public int solution(int n) {
4+
int answer = n+1;
5+
int cnt = countOne(n);
6+
7+
while (answer > n) {
8+
if (countOne(answer) == cnt){
9+
break;
10+
}
11+
answer++;
12+
}
13+
14+
return answer;
15+
}
16+
17+
int countOne(int x) {
18+
String s = Integer.toBinaryString(x);
19+
int cnt = 0;
20+
for (int i = 0; i < s.length(); i++) {
21+
if (s.charAt(i) == '1') cnt++;
22+
}
23+
return cnt;
24+
}
25+
}
26+
```

0 commit comments

Comments
 (0)