Leetcode---剑指Offer题15---二进制中1的个数

Posted Fflyqaq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode---剑指Offer题15---二进制中1的个数相关的知识,希望对你有一定的参考价值。

剑指Offer-面试题15---二进制中1的个数

二进制中1的个数

https://leetcode-cn.com/problems/er-jin-zhi-zhong-1de-ge-shu-lcof/

 

使用一个位运算的小技巧即可。

 

        public int HammingWeight(uint n)
        {
            int count = 0;

            while (n!=0)
            {
                count++;
                n = (n - 1) & n;
            }

            return count;
        }

  

以上是关于Leetcode---剑指Offer题15---二进制中1的个数的主要内容,如果未能解决你的问题,请参考以下文章