Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in read-only mode.

Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).

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

Why this solution is much faster than the non-recursive solution? I'm confused about it... My solution costs 12ms.

class Solution { public: int hammingWeight(uint32_t n) { int ans = 0; while(n) { ans += n & 1; n = n>>1; } return ans; } };

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