C++,3ms,beats100%,n&(n-1)


  • 0
    B

    class Solution {
    public:
    int hammingWeight(uint32_t n) {
    int count = 0;
    while(n)
    {
    n = n&(n-1);
    count++;
    }
    return count;

    }
    

    };


Log in to reply
 

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