class Solution {
public int solution(int n) {
int countN=countOne(n);
int nextN = n+1;
while(countOne(n)!=countOne(nextN)){
nextN++;
}
return nextN;
}
int countOne(int n){
int count = 0;
String str = Integer.toBinaryString(n);
for(int i = 0; i<str.length(); i++){
if(str.charAt(i)=='1') count++;
}
return count;
}
}
* 2진수 : Integer.toBinaryString()
* 8진수 : Integer.toOctalString()
*16진수 : Integer.toHexString()
'NOTE > 프로그래머스' 카테고리의 다른 글
Level2 - 가장 큰 정사각형 찾기 (0) | 2022.03.16 |
---|---|
Level2 - 올바른 괄호 (0) | 2022.03.14 |
Level2 - 문자열 압축 (0) | 2022.03.14 |
Level 2-가장 큰 수 (0) | 2022.03.06 |
22.01.08 - (2) (0) | 2022.01.08 |