Skip to content

Commit 55251ac

Browse files
author
iamminji
committed
[20200705] Solve july challenge questions
1 parent 41f4bb7 commit 55251ac

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,5 @@
227227
| [Arranging Coins](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3377/) | [python](challenge/2020/july/Arranging_Coins.py) | [solutions](challenge/2020/july/Arranging_Coins.md)|
228228
| [Binary Tree Level Order Traversal II](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3378/) | [python](challenge/2020/july/Binary_Tree_Level_Order_Traversal_II.py) | [solutions](challenge/2020/july/Binary_Tree_Level_Order_Traversal_II.md)|
229229
| [Prison Cells After N Days](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3379/) | [python](challenge/2020/july/Prison_Cells_After_N_Days.py) | [solutions](challenge/2020/july/Prison_Cells_After_N_Days.md)|
230-
| [Ugly Number II](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3380/) | [python](challenge/2020/july/Ugly_Number_II.py) | [solutions](challenge/2020/july/Ugly_Number_II.md)|
230+
| [Ugly Number II](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3380/) | [python](challenge/2020/july/Ugly_Number_II.py) | [solutions](challenge/2020/july/Ugly_Number_II.md)|
231+
| [Hamming Distance](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3381/) | [python](challenge/2020/july/Hamming_Distance.py) | [solutions](challenge/2020/july/Hamming_Distance.md)|
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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, calculate the Hamming distance.
5+
6+
Note:
7+
```
8+
0 ≤ x, y < 2^31.
9+
```
10+
11+
Example:
12+
```
13+
Input: x = 1, y = 4
14+
15+
Output: 2
16+
17+
Explanation:
18+
1 (0 0 0 1)
19+
4 (0 1 0 0)
20+
↑ ↑
21+
```
22+
23+
The above arrows point to positions where the corresponding bits are different.
24+
25+
### 솔루션
26+
27+
#### 해결 1
28+
integer 를 bin 메서드로 변경 후 최대 입력 값이 2^31 이므로 31개의 string 으로 만들어주었다.
29+
그리고 값 비교를 누적하여 리턴하였다.
30+
31+
코드는 아래와 같다.
32+
33+
```python3
34+
class Solution:
35+
def hammingDistance(self, x: int, y: int) -> int:
36+
p = bin(x)
37+
q = bin(y)
38+
39+
res = 0
40+
for a, b in zip(("%031d" % int(p[2:])), ("%031d" % int(q[2:]))):
41+
if a != b:
42+
res += 1
43+
return res
44+
45+
```
46+
47+
#### 해결 2
48+
bit 값이 서로 다른 경우의 count 이므로 서로 다를 경우 1 로 해주는 exclusive or (xor) 연산을 해준다.
49+
그리고 1 카운트를 해주었다.
50+
51+
```python3
52+
def hammingDistanceUseXor(self, x: int, y: int) -> int:
53+
p = x ^ y
54+
res = 0
55+
for q in bin(p)[2:]:
56+
if q == "1":
57+
res += 1
58+
return res
59+
```
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def hammingDistanceUseString(self, x: int, y: int) -> int:
3+
p = bin(x)
4+
q = bin(y)
5+
6+
res = 0
7+
for a, b in zip(("%031d" % int(p[2:])), ("%031d" % int(q[2:]))):
8+
if a != b:
9+
res += 1
10+
return res
11+
12+
def hammingDistanceUseXor(self, x: int, y: int) -> int:
13+
p = x ^ y
14+
res = 0
15+
for q in bin(p)[2:]:
16+
if q == "1":
17+
res += 1
18+
return res
19+
20+
21+
if __name__ == '__main__':
22+
sol = Solution()
23+
print(sol.hammingDistanceUseString(1, 4))
24+
print(sol.hammingDistanceUseXor(1, 4))

0 commit comments

Comments
 (0)