-
Notifications
You must be signed in to change notification settings - Fork 0
191_Numberof1Bits
a920604a edited this page Apr 14, 2023
·
1 revision
title: 191. Number of 1 Bits
利用 n&(n-1)
找 n 在二進位表示中有多少個1
class Solution {
public:
int hammingWeight(uint32_t n) {
int ret = 0;
while(n){
ret++;
n &=(n-1);
}
return ret;
}
};
- time complexity
O(1)
- space complexity
O(1)
footer