Skip to main content

191. Number of 1 Bits

Published 1 min readMarkdown

Write a function that takes the binary representation of a positive integer and returns the number of set bits it has (also known as the Hamming weight).

Thoughts

  • This one is basically identical to 2220 kekw

Solution

class Solution {
    public int hammingWeight(int n) {
        return Integer.bitCount(n);
    }
}

Time Complexity: O(1)
Space Complexity: O(1)

Conclusion

umm, these bit manipulation ones are pretty easy. I suppose I should find a way to calculate this myself and not use a built in function to do it. I can see providing this answer in an interview and being asked to provide a correct answer to it.

No comments yet. Be the first to comment.