[프로그래머스] 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 정답”의 390개의 댓글

  1. Как [url=https://top-seo-prodvizhenie.ru]топ seo продвижение[/url] работает при частых апдейтах алгоритмов?

  2. Как составить грамотное ТЗ, прежде чем [url=https://zakazat-prodvizhenie-sajta.ru]заказать продвижение сайта[/url]?

  3. 핑백: otitis media symptom list

  4. I live on a small island here so entertainment options are limited but this app brings the casino right to my pocket. I log in after helping my family with the fishing nets and the games load without eating up all my data. The daily rewards keep me motivated and I finally feel like a valued player instead of just another number. action777app

  5. I have been looking for a trustworthy gaming site for years and I am so glad I stumbled upon this one. The login never crashes even during heavy rain season and the customer care team actually listens to our suggestions. I bring my tablet to the local carinderia and play between orders. It feels like a homegrown platform that truly understands us. phtg777

  6. Hey there, I just stumbled upon this platform and honestly it feels like a breath of fresh air. The interface is super smooth and the games load lightning fast without any annoying glitches. I usually play late at night and the customer support is always awake to help. Definitely my new go to spot for some chill spins and real wins. Visit ph69jl if you want the same vibe.

  7. [368]wtc88 Official Login | Trusted Online Casino Malaysia Fast Payout,wtc88 offers a secure platform for the best slot games with fast payouts. Download the app today to explore live baccarat and start playing with a trusted leader. visit: wtc88

댓글 달기

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