C# recursive using Math.Log


  • 1
    D
    public class Solution {
        public int HammingWeight(uint n) {
            if (n==1 || n == 1){
                return n;
            }
            if (n==0){
                return 0;
            }
            var lg = (uint)(Math.Log(n)/Math.Log(2));
            return 1 + HammingWeight((uint)(n - Math.Pow(2,lg)));
        }
    }

Log in to reply
 

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