n&1(C++,3ms)


  • 0
    N
    class Solution {
    public:
        int hammingWeight(uint32_t n) {
            int ans=0;
            while(n)
            {
                ans+=(n&1);
                n>>=1;
            }
            return ans;
        }
    };

Log in to reply
 

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