4ms c solution,divide n by 2 for times


  • 0

    int hammingWeight(uint32_t n) {
    int j=0;
    if(n==0||n==1)
    return n;

    while(n/2!=1)
    {
        if(n%2==1)
          j++;
        n=n/2;
        
    }
    

    if(n/2==1&&n%2==1)
    return (j+2);
    else
    return (j+1);
    }


Log in to reply
 

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