leetcode每日刷题计划-简单篇day23

昨天没有电脑,ipad&蓝牙键盘敲题

打卡继续

Num 191 位1的个数

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int a=1;
        int count=a&n;
        for(int i=1;i<=31;i++)
        {
            a=a<<1;
            if(a & n) count++;
        }
        return count;
    }
};
View Code

本身没有坑点,然后用x<<1就可以,不需要使用32数组(蠢死我)

位运算本身就很简单,还有技巧,回头补

时间才能证明一切,选好了就尽力去做吧!
原文地址:https://www.cnblogs.com/tingxilin/p/11179744.html