C++ solution needs 10ms


  • 1
    V
    int hammingWeight(uint32_t n) {
    		int result = 0;
    		uint32_t mask = 0X80000000;
    		for(int i = 0; i < 32; i++){
    			uint32_t temp = n << 31;
    			if ( (temp ^ mask) == 0)
    				result++;
    			n = n >> 1;
    			}
    		return result;
        }

  • 1
    P
    int hammingWeight(uint32_t n){
    	int k,c,s = 0;
    	for (c = 0; c < 32 ;c++)
    	{
    		k = n >> c;
    		if (k & 1)
    			s++;
    	}
    	return s;
    }

Log in to reply
 

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