191. Number of 1 Bits
problem
code
class Solution { public: int hammingWeight(uint32_t n) { int res = 0; while(n) { if(n%2) res++; n /= 2; } return res; } };
solution2
依据二进制数据的性质,使用模板进行实现。
class Solution { public: int hammingWeight(uint32_t n) { int bits = 0; int mask = 1; for(int i=0; i<32; i++) { if(n&mask) bits++; mask *=2; } return bits; } };
各美其美,美美与共,不和他人作比较,不对他人有期待,不批判他人,不钻牛角尖。
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/