Skip to content

Commit 169d3f3

Browse files
committed
Hamming distance.
1 parent a1d0744 commit 169d3f3

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
3+
4+
Given two integers x and y, return the Hamming distance between them.
5+
6+
 
7+
8+
Example 1:
9+
Input: x = 1, y = 4
10+
Output: 2
11+
Explanation:
12+
1 (0 0 0 1)
13+
4 (0 1 0 0)
14+
↑ ↑
15+
The above arrows point to positions where the corresponding bits are different.
16+
17+
Example 2:
18+
Input: x = 3, y = 1
19+
Output: 1
20+
 
21+
22+
Constraints:
23+
- 0 <= x, y <= 231 - 1
24+
*/
25+
class Solution {
26+
func hammingDistance(_ x: Int, _ y: Int) -> Int {
27+
if x == y { return 0 }
28+
var i = 0
29+
var j = 0
30+
while pow(2, i) <= x {
31+
i += 1
32+
}
33+
i -= 1
34+
var r = decimalToBinary(x, maxBit: i)
35+
36+
while pow(2, j) <= y {
37+
j += 1
38+
}
39+
j -= 1
40+
var t = decimalToBinary(y, maxBit: j)
41+
r = r.reversed()
42+
t = t.reversed()
43+
if r.count < t.count {
44+
let temp = r
45+
r = t
46+
t = temp
47+
}
48+
let delta = abs(r.count - t.count)
49+
if delta > 0 {
50+
for _ in 0..<delta {
51+
t.insert(0, at: 0)
52+
}
53+
}
54+
55+
var times = 0
56+
for i in 0..<r.count {
57+
let e1 = r[i]
58+
let e2 = t[i]
59+
if e1 != e2 {
60+
times += 1
61+
}
62+
}
63+
return times
64+
}
65+
66+
private func pow(_ base: Int, _ power: Int) -> Int {
67+
if base == 0 { return 0 }
68+
var res = 1
69+
for _ in 0..<power {
70+
res *= base
71+
}
72+
return res
73+
}
74+
75+
private func decimalToBinary(_ num: Int, maxBit bit: Int) -> [Int] {
76+
var n = num
77+
var i = bit
78+
var arr = Array(repeating: 0, count: bit + 1)
79+
while i >= 0 && n > 0 {
80+
while pow(2, i) <= n {
81+
n -= pow(2, i)
82+
arr[i] = 1
83+
}
84+
i -= 1
85+
}
86+
return arr
87+
}
88+
}
89+
90+
let s = Solution()
91+
let r = s.hammingDistance(333, 654)
92+
print(r)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/461.Hamming Distance.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
89. [Minimum Moves to Equal Array Elements](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/448.Minimum%20Moves%20toEqual%20Array%20Elements.playground/Contents.swift)
9494
90. [Assign Cookies](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/455.Assign%20Cookies.playground/Contents.swift)
9595
91. [Repeated Substring Pattern](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/459.Repeated%20Substring%20Pattern.playground/Contents.swift)
96+
92. [Hamming Distance](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/461.Hamming%20Distance.playground/Contents.swift)
9697

9798
#### Medium
9899

0 commit comments

Comments
 (0)