My time complexity optimized solution(from Sword at Offer)


  • 1
    W
    class Solution {
    public:
        int hammingWeight(uint32_t n) {
            int count = 0;
            
            while(n){
                ++count;
                n = (n - 1) & n;
            }
            return count;
        }
    };

Log in to reply
 

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