leetcode 191. 位1的个数(java & python3)
java:
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int res = 0;
for(int i = 0; i < 32; i++){
if((n & (1 << i)) != 0){
res ++;
}
}
return res;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int res = 0;
while(n != 0){
res ++;
n &=
共有 0 条评论