191. Number of 1 Bits 發表於 2023-02-13 | 分類於 leetcode problemsolution利用 n&(n-1) 找 n 在二進位表示中有多少個1 1234567891011class Solution {public: int hammingWeight(uint32_t n) { int ret = 0; while(n){ ret++; n &=(n-1); } return ret; }}; analysis time complexity O(1) space complexity O(1)