알고리즘/코딩테스트

[leetcode] number of 1 bits

호두밥 2022. 4. 21. 23:38

https://leetcode.com/problems/number-of-1-bits/

 

Number of 1 Bits - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        String s = Integer.toBinaryString(n); 
        return (s.replace("0","")).length();
    }
}