题解 | #二进制中1的个数#

二进制中1的个数

http://www.nowcoder.com/practice/8ee967e43c2c4ec193b040ea7fbb10b8

```function NumberOf1(n)
{
    // write code here
    // https://www.cnblogs.com/skillking/p/9930095.html   
    let num = 0;
    while(n){
        num++;
        n = n & (n - 1) //此公式可以消除最右边的1
    }
    return num
}
module.exports = {
    NumberOf1 : NumberOf1
};
全部评论

相关推荐

05-22 09:23
门头沟学院 Java
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务