美文网首页
191. Number of 1 Bits

191. Number of 1 Bits

作者: hyhchaos | 来源:发表于2016-12-06 11:23 被阅读13次

    Java

    public class Solution {
        // you need to treat n as an unsigned value
        public int hammingWeight(int n) {
            int count=0;
            while(n!=0)
            {
                n=n&(n-1);
                count++;
            }
            return count;
        }
    }
    

    相关文章

      网友评论

          本文标题:191. Number of 1 Bits

          本文链接:https://www.haomeiwen.com/subject/vrfrmttx.html