[프로그래머스] Lv.2 다음 큰 숫자.java

hgghfgf·2023년 5월 11일
0

프로그래머스

목록 보기
42/227

2 다음 큰 숫자.java

class Solution {
    public int solution(int n) {
        int answer = 0;
        int nowCount = Integer.bitCount(n);

        while(true){
            n++;
            int nextCount = Integer.bitCount(n);

            if(nowCount == nextCount){
                break;
            }
        }

        answer = n;

        return answer;
    }
}

n의 2진수 표현에서 1의 갯수를 구한 다음, n보다 큰 수 중 1의 갯수가 같은 가장 작은 수를 찾아서 반환

출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges

0개의 댓글