4ms C++ Solution Using Easy Recursion


  • 0
    C
    class Solution {
    public:
        int hammingWeight(uint32_t n) {
            if(n <= 1) return n;
            else{
                if (n%2 == 1) return (hammingWeight(n/2)+1);
                else return hammingWeight(n/2);
            }
        }
    };

Log in to reply
 

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