We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5b703c5 commit df0a013Copy full SHA for df0a013
solution/0461.Hamming Distance/README_EN.md
@@ -0,0 +1,17 @@
1
+# Hamming Distance
2
+
3
+The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
4
5
+Given two integers _x_ and _y_, calculate the Hamming distance.
6
7
+## Example 1:
8
+```
9
+ Input: x = 1, y = 4
10
+ Output: 2
11
12
+ Explanation:
13
+ 1 (0 0 0 1)
14
+ 4 (0 1 0 0)
15
+ ↑ ↑
16
+ The above arrows point to positions where the corresponding bits are different.
17
solution/0461.Hamming Distance/Solution.java
@@ -0,0 +1,10 @@
+class Solution {
+ public int hammingDistance(int x, int y) {
+ int count = 0, sum = x ^ y;
+ while (sum != 0) {
+ sum &= (sum - 1);
+ count++;
+ }
+ return count;
+}
0 commit comments