[Programmers] 다음 큰 숫자 (12911번) - Java Solution

문제

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

Solution

class Solution {
    int getOneCountOfDecimal(int n) {
        int cnt = 0;
        
        while(n > 0) {
            int rest = n % 2;
            
            if(rest == 1) {
                cnt++;
            }
            
            n /= 2;
        }
        
        return cnt;
    }
    
    public int solution(int n) {
        int answer = 0;
        int targetOneCnt = getOneCountOfDecimal(n);
        
        while(n < 1_000_001) {
            n ++;
            
            if(targetOneCnt == getOneCountOfDecimal(n)) {
                return n;
            }
        }
        
        return answer;
    }
}

Leave a comment