1 class Solution { 2 public: 3 int hammingWeight(uint32_t n) { 4 int result = 0; 5 while (n > 0) { 6 if (n & 1) { 7 result++; 8 } 9 n >>= 1; 10 } 11 return result; 12 } 13 };
时间: 2024-10-12 09:20:31
1 class Solution { 2 public: 3 int hammingWeight(uint32_t n) { 4 int result = 0; 5 while (n > 0) { 6 if (n & 1) { 7 result++; 8 } 9 n >>= 1; 10 } 11 return result; 12 } 13 };