File tree 1 file changed +11
-34
lines changed
src/main/java/com/fishercoder/solutions
1 file changed +11
-34
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
2
3
- /**
4
- * 461. Hamming Distance
5
- *
6
- * The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
7
-
8
- Given two integers x and y, calculate the Hamming distance.
9
-
10
- Note:
11
- 0 ≤ x, y < 231.
12
-
13
- Example:
14
-
15
- Input: x = 1, y = 4
16
-
17
- Output: 2
18
-
19
- Explanation:
20
- 1 (0 0 0 1)
21
- 4 (0 1 0 0)
22
- ↑ ↑
23
-
24
- The above arrows point to positions where the corresponding bits are different.
25
- */
26
3
public class _461 {
27
- public static class Solution1 {
28
- public int hammingDistance (int x , int y ) {
29
- int n = x ^ y ;
30
- int count = 0 ;
31
- while (n != 0 ) {
32
- count ++;
33
- n &= (n - 1 );
34
- }
35
- return count ;
36
- }
37
- }
4
+ public static class Solution1 {
5
+ public int hammingDistance (int x , int y ) {
6
+ int n = x ^ y ;
7
+ int count = 0 ;
8
+ while (n != 0 ) {
9
+ count ++;
10
+ n &= (n - 1 );
11
+ }
12
+ return count ;
13
+ }
14
+ }
38
15
}
You can’t perform that action at this time.
0 commit comments