4ms c++ non-recursive solution, bit operation.


  • 0
    S
    class Solution {
    public:
        int hammingWeight(uint32_t n) {
            
            int count=0;
            
            for(int i=0;i<32;++i)
                if((n>>i)&1)
                    count++;
            return count;
            
        }
    };

Log in to reply
 

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