提交 5d3f4d15 编写于 作者: GreyZeng's avatar GreyZeng

update code

上级 83053c5e
......@@ -8,14 +8,15 @@ package git.snippet.bit;
// 如果多次调用这个函数,你将如何优化你的算法?
// LeetCode: https://leetcode.com/problems/number-of-1-bits/
public class LeetCode_0191_NumberOf1Bits {
// TODO
// 最优解
public static int hammingWeight(int n) {
n = (n & 0x55555555) + ((n >>> 1) & 0x55555555);
n = (n & 0x33333333) + ((n >>> 2) & 0x33333333);
n = (n & 0x0f0f0f0f) + ((n >>> 4) & 0x0f0f0f0f);
n = (n & 0x00ff00ff) + ((n >>> 8) & 0x00ff00ff);
n = (n & 0x0000ffff) + ((n >>> 16) & 0x0000ffff);
n = (n & 0b01010101_01010101_01010101_01010101) + ((n >>> 1) & 0b01010101_01010101_01010101_01010101);
n = (n & 0b00110011_00110011_00110011_00110011) + ((n >>> 2) & 0b00110011_00110011_00110011_00110011);
n = (n & 0b00001111_00001111_00001111_00001111) + ((n >>> 4) & 0b00001111_00001111_00001111_00001111);
n = (n & 0b00000000_11111111_00000000_11111111) + ((n >>> 8) & 0b00000000_11111111_00000000_11111111);
n = (n & 0b00000000_00000000_11111111_11111111) + ((n >>> 16) & 0b00000000_00000000_11111111_11111111);
return n;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册