[프로그래머스] 12911 다음 큰 숫자 / Java 정답

programmers

쉬운 문제라도 문제를 푸는 방법은 다양합니다.

더 명확하고 깔끔한 문제 해결 방법을 아신다면 자유롭게 말씀해주세요.

프로그래머스 번호 제목

프로그래머스 저작권을 침해하지 않기 위해

문제 보시고 싶으시면 버튼을 눌러주세요.

답변 형식
프로그래머스 12911 자바
프로그래머스 12911 파이썬

숫자 N을 이진수로 바꾸었을 때 1의 갯수가 같고, N보다 큰 최소한의 정수를 리턴하면 된다.

풀이 조건

자바 문법으로 Integer.bitCount() 함수를 알고 있으면 쉽게 해결된다.

숫자를 이진수로 바꿔주고, 이진수의 1의 갯수를 세주는 함수를 통해 빠르게 이진수의 1  갯수를 알 수 있다.

class Solution {
        public int solution(int n) {        
            int n_num = Integer.bitCount(n);
                       
            int k = n+1;
           
            while(true) {
                int k_num = Integer.bitCount(k);                
               
                if(n_num == k_num) {
                    break;
                }
               
                k++;
            }
            return k;
        }
    }
class Solution {
    // Integer.bitCount : 비트로 변환했을 때 1의 갯수 반환
    public int solution(int n) {    
           
        int n_num = Integer.bitCount(n);
        // 주어진 숫자의 비트 형식 1의 갯수 세기
                   
        int k = n+1;
        // n보다 큰 임의의 정수 K
       
        while(true) {
            int k_num = Integer.bitCount(k);
            // K의 비트 형식 1의 갯수 세기                
           
            if(n_num == k_num) {
                break;
                // n과 k의 비트 형식 1 갯수가 같으면 반환
            }
           
            k++;
            // 비트 형식 1 갯수가 다르다면 K의 크기 1 추가
        }
        return k;
    }
}

“[프로그래머스] 12911 다음 큰 숫자 / Java 정답”의 436개의 댓글

  1. Mobile betting can be such a headache but this spot makes it feel effortless. I drop by during my lunch break and everything loads in a flash on my phone. Withdrawals hit my account faster than I expect and I never worry about my details being safe. Really appreciate the chill vibe and honest gameplay. pk6sixs

  2. I have been playing here for over a year and the experience just keeps getting better. The live dealer tables feel so authentic and the chat with dealers makes it feel like hanging out with friends. Reliable payments and fair gameplay every single time. 789betlogin

  3. [9431]Zorototo: Login Daftar Akun Slot Gacor Deposit Dana,Zorototo menyediakan akses login dan daftar akun mudah. Nikmati slot gacor deposit dana serta download apk slot terpercaya sekarang juga di sini. visit: zorototo

  4. [8243]68mx Login Oficial | Descarga la APK y las Mejores Slots en México,68mx es tu portal de entretenimiento favorito. Disfruta depósitos rápidos con SPEI, aprende cómo jugar ruleta online y gana en grande. Regístrate ahora mismo. visit: 68mx

댓글 달기

이메일 주소는 공개되지 않습니다.