0ms C solution with recursion.


  • 0
    H
    int hammingWeight(uint32_t n) {
        if (n<1) return  0;
            return (n==1? 1:((n%2)+ hammingWeight(n/2)));
    }
    
    

  • 0
    T

    It's no need to determine if n == 1, more concise code is:

    int hammingWeight_recursive(uint32_t n) {
            if (n == 0) return 0;
            return n%2 + hammingWeight(n/2);
    }
    

Log in to reply
 

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