Deep_Dev
article thumbnail

https://school.programmers.co.kr/learn/courses/30/lessons/12911

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 

 


 

 

class Solution {
    public int solution(int n) {
        int answer = 0;

        String temp = Integer.toBinaryString(n);
        int tempCount = temp.length() - temp.replace("1", "").length();

        int num = n+1;
        while(true){
            String num_temp = Integer.toBinaryString(num);
            int num_count = num_temp.length() - num_temp.replace("1","").length();
            if(tempCount == num_count) {
                answer = num;
                break;
            }
            else {
                num++;
            }
        }
        
        return answer;
    }
}