public int hammingWeight(int n) {
int count = 0;
while (n != 0) {
count += n & 1;
n = n >>> 1;
}
return count;
}
public int hammingWeight_2(int n) {
String s = Integer.toBinaryString(n);
int count = 0;
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) == '1') {
count++;
}
}
return count;
}
LeetCode-Algorithms-[Easy]面试题15. 二进制中1的个数
猜你喜欢
转载自blog.csdn.net/m0_37302219/article/details/105513190
今日推荐
周排行