-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path461.cpp
More file actions
45 lines (37 loc) · 1002 Bytes
/
461.cpp
File metadata and controls
45 lines (37 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public:
string toBinary(int num){
string ans = "";
while(num > 1){
ans = to_string(num%2) + ans;
num /= 2;
}
ans = to_string(num) + ans;
return ans;
}
int hammingDistance(int x, int y) {
string num1 = toBinary(x);
string num2 = toBinary(y);
if(num1.length() != num2.length()){
int dif = num1.length() - num2.length();
if(dif < 0){
while(dif != 0){
num1 = "0" + num1;
dif++;
}
}else{
while(dif != 0){
num2 = "0" + num2;
dif--;
}
}
}
int count =0;
for(int i = 0; i <num1.length(); i++){
if(num1[i] != num2[i]){
count++;
}
}
return count;
}
};