6ms 4 lines c++ solution easy understand

  • 1
    int hammingWeight(uint32_t n) {
        int counter = 0;
        for(int i = 0; i<=31; i++)
            if(((1<<i)&(n)) != 0) counter++;
        return counter;

    just iteratively check each bit to see if it is a 1. counter +1 if it is a 1.

Log in to reply

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