C# solution: loop


  • 0
    B
    public class Solution 
    {
        public int HammingWeight(uint n) 
        {
            int count = 0;
            int digit = 31;
            while(digit >= 0)
            {  
                if ((n & 1) == 1)
                {
                    count++;
                }
    
                n >>= 1;
                digit--;
            }
            return count;
        }
    }
    

Log in to reply
 

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