标签:problems leetcode nbsp onclick 技术 ++ close etc while
维护匹配串 从1开始逐位移动比较
class Solution { public: int hammingWeight(uint32_t n) { // uint32_t为32位无符号类型数据 int count=0; uint32_t donser=1; for(int i=0;i<32;i++) { if((n&donser)>=1) count++; donser<<=1; } return count; } };
n与n-1取&
class Solution { public: int hammingWeight(uint32_t n) { int count=0; while(n) { n=n&(n-1); count++; } return count; } };
调用内置函数 __builtin_popcount(n)
class Solution { public: int hammingWeight(uint32_t n) { // uint32_t为32位无符号类型数据 return __builtin_popcount(n); } };
标签:problems leetcode nbsp onclick 技术 ++ close etc while
原文地址:https://www.cnblogs.com/dzzy/p/12297911.html