不论难易,每道题用python和C都实现一遍是目标
这道题就比较简单了,一次到位
int hammingWeight(uint32_t n){
int i = 0;
while (n){
if (n % 2)
++i;
n = n / 2;
}
return i;
}
C的性能就是好
600 / 600 test cases passed. Status: Accepted Runtime: 1 ms
本来就已经1ms了,但是看到了除法,想想改成移位试试
int hammingWeight(uint32_t n){
int i = 0;
while (n){
if (n % 2)
++i;
n = n >> 1;
}
return i;
}
奇怪的是,居然性能弱了点!!
600 / 600 test cases passed. Status: Accepted Runtime: 2 ms
