Skip to content

Commit

Permalink
LeetCode461.汉明距离
Browse files Browse the repository at this point in the history
  • Loading branch information
yangxuechen committed Jun 30, 2018
1 parent 15bd6bd commit 3b61f5c
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions LeetCode1/LeetCode461.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package leetCode;

public class LeetCode461 {

public static void main(String[] args) {
// TODO Auto-generated method stub
String s=toBit(1);
System.out.println(s);
String s2=toBit(4);
System.out.println(s2);
int n=hammingDistance(1,4);
System.out.println(n);
}
public static int hammingDistance(int x, int y) {
String s1=toBit(x);
String s2=toBit(y);
int len1=s1.length();
int len2=s2.length();
int count=0;
len1--;
len2--;
while(len1>=0&&len2>=0) {
if(s1.charAt(len1)!=s2.charAt(len2)) {
count++;
}
len1--;
len2--;
}
while(len1>=0) {
if(s1.charAt(len1)=='1') {
count++;
}
len1--;
}
while(len2>=0) {
if(s2.charAt(len2)=='1') {
count++;
}
len2--;
}
return count;
}

public static String toBit(int n) {
if(n==0) {
return "0";
}
String s="";
while(n>0) {
int temp=n%2;
s=temp+s;
n/=2;
}
return s;
}
}

0 comments on commit 3b61f5c

Please sign in to comment.