-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path461.汉明距离.js
54 lines (46 loc) · 915 Bytes
/
461.汉明距离.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* @lc app=leetcode.cn id=461 lang=javascript
*
* [461] 汉明距离
*/
// @lc code=start
/**
* @param {number} x
* @param {number} y
* @return {number}
*/
// 动态规划(内存溢出)
// var hammingDistance = function(x, y) {
// let z = x ^ y
// let dp = [0]
// for (let i = 1; i <= z; i++) {
// if (i % 2 === 0) {
// dp[i] = dp[i / 2]
// } else {
// dp[i] = dp[i - 1] + 1
// }
// }
// return dp[z]
// };
// 右移 + 换位
// var hammingDistance = function (x, y) {
// let z = x ^ y
// let res = 0
// while (z !== 0) {
// res += z & 1
// z >>= 1
// }
// return res
// }
// Brain Kernighan 算法
// let y = x & (x - 1),则 y 恰为 x 删去其二进制表示中最右侧的 1 的结果
var hammingDistance = function (x, y) {
let z = x ^ y
let res = 0
while (z !== 0) {
z &= z - 1
res++
}
return res
}
// @lc code=end