728x90
문제

풀이
class Solution {
public int solution(int n) {
int answer = 0;
int a=Integer.bitCount(n); //2진수 변환 후 1의 개수 반환
int k=n+1;
while(true){
if(a==Integer.bitCount(k)){ //1의 개수가 같다면
answer=k;
break;
}
k++;
}
return answer;
}
}
Integer.bitCount()
더보기
num 숫자를 2진수로 변환 후 1의 개수 반환하는 메서드
int num=4;
int N =Integer.bitCount(num);
// 4 -> 0100 (2진수 변환) -> 1개(1의 개수 반환)
출력결과
1
728x90
'프로그래머스 & 백준' 카테고리의 다른 글
[프로그래머스/JAVA] - 기능개발 (0) | 2023.05.04 |
---|---|
[프로그래머스/JAVA] - 피보나치의 (0) | 2023.04.25 |
[프로그래머스/JAVA] - 숫자의 표현 (0) | 2023.04.24 |
[프로그래머스/JAVA] - 이진 변환 반복하기 (0) | 2023.04.24 |
[프러그래머스/JAVA] - JadenCase 문자열 만들기 (0) | 2023.04.18 |
728x90
문제

풀이
class Solution {
public int solution(int n) {
int answer = 0;
int a=Integer.bitCount(n); //2진수 변환 후 1의 개수 반환
int k=n+1;
while(true){
if(a==Integer.bitCount(k)){ //1의 개수가 같다면
answer=k;
break;
}
k++;
}
return answer;
}
}
Integer.bitCount()
더보기
num 숫자를 2진수로 변환 후 1의 개수 반환하는 메서드
int num=4;
int N =Integer.bitCount(num);
// 4 -> 0100 (2진수 변환) -> 1개(1의 개수 반환)
출력결과
1
728x90
'프로그래머스 & 백준' 카테고리의 다른 글
[프로그래머스/JAVA] - 기능개발 (0) | 2023.05.04 |
---|---|
[프로그래머스/JAVA] - 피보나치의 (0) | 2023.04.25 |
[프로그래머스/JAVA] - 숫자의 표현 (0) | 2023.04.24 |
[프로그래머스/JAVA] - 이진 변환 반복하기 (0) | 2023.04.24 |
[프러그래머스/JAVA] - JadenCase 문자열 만들기 (0) | 2023.04.18 |