Skip to content

Commit d092de6

Browse files
committed
feat: add solutions to lc problem: No.0461. Hamming Distance
1 parent 07b3fb1 commit d092de6

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

solution/0400-0499/0461.Hamming Distance/README.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,55 @@
3333

3434
<!-- 这里可写通用的实现逻辑 -->
3535

36+
利用异或运算的规律找出不同的位
37+
38+
- 0 ^ 0 = 0
39+
- 1 ^ 1 = 0
40+
- 0 ^ 1 = 1
41+
- 1 ^ 0 = 1
42+
3643
<!-- tabs:start -->
3744

3845
### **Python3**
3946

4047
<!-- 这里可写当前语言的特殊实现逻辑 -->
4148

4249
```python
43-
50+
class Solution:
51+
def hammingDistance(self, x: int, y: int) -> int:
52+
num, count = x ^ y, 0
53+
while num != 0:
54+
num &= num - 1
55+
count += 1
56+
return count
4457
```
4558

4659
### **Java**
4760

4861
<!-- 这里可写当前语言的特殊实现逻辑 -->
4962

5063
```java
64+
class Solution {
65+
public int hammingDistance(int x, int y) {
66+
int num = x ^ y;
67+
int count = 0;
68+
while (num != 0) {
69+
num &= num - 1;
70+
count++;
71+
}
72+
return count;
73+
}
74+
}
75+
```
76+
77+
或者利用库函数 `Integer.bitCount()`
5178

79+
```java
80+
class Solution {
81+
public int hammingDistance(int x, int y) {
82+
return Integer.bitCount(x ^ y);
83+
}
84+
}
5285
```
5386

5487
### **...**

solution/0400-0499/0461.Hamming Distance/README_EN.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,51 @@ The above arrows point to positions where the corresponding bits are different.
3838

3939
## Solutions
4040

41+
Use xor operation to find different bits.
42+
43+
- 0 ^ 0 = 0
44+
- 1 ^ 1 = 0
45+
- 0 ^ 1 = 1
46+
- 1 ^ 0 = 1
47+
4148
<!-- tabs:start -->
4249

4350
### **Python3**
4451

4552
```python
46-
53+
class Solution:
54+
def hammingDistance(self, x: int, y: int) -> int:
55+
num, count = x ^ y, 0
56+
while num != 0:
57+
num &= num - 1
58+
count += 1
59+
return count
4760
```
4861

4962
### **Java**
5063

5164
```java
65+
class Solution {
66+
public int hammingDistance(int x, int y) {
67+
int num = x ^ y;
68+
int count = 0;
69+
while (num != 0) {
70+
num &= num - 1;
71+
count++;
72+
}
73+
return count;
74+
}
75+
}
76+
```
77+
78+
Or use the library function `Integer.bitCount()`
5279

80+
```java
81+
class Solution {
82+
public int hammingDistance(int x, int y) {
83+
return Integer.bitCount(x ^ y);
84+
}
85+
}
5386
```
5487

5588
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
class Solution {
22
public int hammingDistance(int x, int y) {
3-
int count = 0, sum = x ^ y;
4-
while (sum != 0) {
5-
sum &= (sum - 1);
3+
// return Integer.bitCount(x ^ y);
4+
int num = x ^ y;
5+
int count = 0;
6+
while (num != 0) {
7+
num &= num - 1;
68
count++;
79
}
810
return count;
911
}
10-
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def hammingDistance(self, x: int, y: int) -> int:
3+
num, count = x ^ y, 0
4+
while num != 0:
5+
num &= num - 1
6+
count += 1
7+
return count

0 commit comments

Comments
 (0)