C code within 4ms


  • 0
    H
    int hammingWeight(uint32_t n) {
    	int result = 0;
    	for(n) {
    		result += n%2;
    		n /= 2;
    	}
    }

  • 1
    Y
    int hammingWeight(uint32_t n) {
        int ret = 0;
        while(n)
        {
            n&=n-1;
            ++ret;
        }
        return ret;
    }
    

    i think this is more efficient.


Log in to reply
 

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