Super simple Java solution

  • 13

    This is about the easiest possible answer in Java :)

    public class Solution {
        // you need to treat n as an unsigned value
        public int hammingWeight(int n) {
            return Integer.bitCount(n);

  • 3

    Good answer! But just want people know that in an real interview the interviewer may ask for another solution that demonstrates algorithmic thinking and ability to handle bitwise operations. Anyway, upvote!

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.