-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15bd6bd
commit 3b61f5c
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |