본문 바로가기
문제풀이

Leetcode Number of 1 Bits JAVA

by AndoneKwon 2021. 1. 4.
public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        String number = Integer.toBinaryString(n);
        int answer = 0;
        for(int i=0;i<number.length();i++){
            if(number.charAt(i)=='1')
                answer++;
        }

        return answer;
    }
}