Love丶FFC's Blog
两个整数的Hamming距离是对应比特位不同的个数。
给定两个整数x和y,计算两者的Hamming距离。
0 ≤ x
, y
< 231
样例1
输入: x = 1 和 y = 4
输出: 2
解释:
1的二进制表示是001
4的二进制表示是100
共有2位不同
样例2
输入: x = 5 和 y = 2
输出: 3
解释:
5的二进制表示是101
2的二进制表示是010
共有3位不同
1.因为二进制数在Java中会自动消去前面的0,因此需要先将两个二进制数的长度补齐。例如1(10)=001(2),但在Java中会消去两个0,因而只显示1。
2.判断两个字符串的长度,例如A为0001,B为1000,则A需要补三个0才能对齐长度。补齐的长度根据两个字符串默认长度之差的绝对值。
3.补足0之后再将默认的二进制值紧随其后。
4.逐一比较各位是否相同。
package LintCode_Easy; public class _835{ public static void main(String[] args) { int x = 5,y = 2; String xs="",ys=""; int total = 0; for(int i=0;i<Math.abs((Integer.toBinaryString(x)).length()-(Integer.toBinaryString(y)).length());i++) { if((Integer.toBinaryString(x)).length()>(Integer.toBinaryString(y)).length()) { ys+="0"; } else { xs+="0"; } } xs+=Integer.toBinaryString(x); ys+=Integer.toBinaryString(y); for(int i=0;i<xs.length();i++) { if(xs.charAt(i)!=ys.charAt(i)) total++; } System.out.println(total); } }
public class Solution { public int hammingDistance(int x, int y) { String xs="",ys=""; int total = 0; for(int i=0;i<Math.abs((Integer.toBinaryString(x)).length()-(Integer.toBinaryString(y)).length());i++) { if((Integer.toBinaryString(x)).length()>(Integer.toBinaryString(y)).length()) { ys+="0"; } else { xs+="0"; } } xs+=Integer.toBinaryString(x); ys+=Integer.toBinaryString(y); for(int i=0;i<xs.length();i++) { if(xs.charAt(i)!=ys.charAt(i)) total++; } return total; } }
Abupguele
Your interventional radiologist will place the biopsy needle into your skin and check its position with an MRI, CT, ultrasound, or fluoroscopy cialis generic cost The nurse should provide the client with which instruction
invoimi
Breast Feeding Reduces Maternal Lower Body Fat, Journal of the American Dietetic Association 93, no closest thing to levitra
MaryLit
fluoxetine 60 mg price
JosephKip
clindamycin 300 mg
JoeLit
cheap levitra for sale
SueLit
clomid clomiphene
Michaelsmopy
tadalafil 10 mg tablet
DarrylBox
can you buy amoxicillin over the counter in australia
MaryLit
zanaflex for headaches
JosephKip
buy synthroid australia
SueLit
effexor prescription cost
SueLit
purchase albendazole online
JosephKip
feldene gel 112g
DavisIgnip
tretinoin cream discount coupon
Michaelsmopy
price lyrica 300 mg
DavisIgnip
buy diflucan canada
DarrylBox
bupropion 189
DavisIgnip
medicine phenergan
SueLit
metformin 19.99
JackLit
canadian pharmacy no prescription
MaryLit
azithromycin rx coupon
AshLit
dapoxetine tablet buy online
Davidevelf
dexamethasone 50 mg
SamuelEcors
prednisolone 5mg without prescription
EyeLit
atarax australia
EyeLit
canada pharmacy online
Davidevelf
robaxin 750 mg uk
Michaelsmopy
retin a .05 cream
Davidevelf
prednisolone cost in india
IvyLit
dexamethasone tablets cost
SueLit
atarax generic brand
SueLit
triamterene-hctz 37.5-25 mg tb
SueLit
cheap cialis 5mg australia
Michaelsmopy
discount cialis canada
DarrylBox
purchase levaquin
DarrylBox
generic atarax
Elwoodexcix
prazosin price
Davidevelf
tamoxifen brand name
Elwoodexcix
triamterene
DavisIgnip
buy robaxin without prescription
SueLit
15 tretinoin
Davidevelf
buy atarax 25mg
JackLit
canadianpharmacymeds
MaryLit
prazosin 10 mg
AshLit
drug furosemide 40mg
SamuelEcors
cialis daily use 5mg
SamuelEcors
order prednisolone online
IvyLit
where can you get azithromycin over the counter
SamuelEcors
online pharmacy non prescription drugs
Michaelsmopy
retin a 0.025 cream price
Williamjotly
atarax over the counter uk
Elwoodexcix
tadalafil – generic
SamuelEcors
buy generic tadalafil in us
CurtisHed
phenergan cream
Davidevelf
robaxin 10mg
DavisIgnip
triamterene 37.5 mg hctz 25mg caps
IvyLit
prazosin 5 mg cap
IvyLit
triamterene tablets
EyeLit
tretinoin 025
DarrylBox
buy atenolol online canada
Williamjotly
robaxin 75 mg
Richardatone
atarax buy
AlanLit
lipitor generic canada
MaryLit
cymbalta capsule
JosephKip
buy robaxin no prescription
Michaelsmopy
atarax price in india
SamLit
atenolol 50 price
ZakLit
retin a 06
DavisIgnip
retin a 05 cost
SamuelEcors
best canadian online pharmacy cialis
ZakLit
can i buy nolvadex with paypal
Davidevelf
rx pharmacy online 24
AlanLit
lipitor prices compare
IvyLit
atarax for hives
DavisIgnip
pharmacy rx world canada
ZakLit
tretinoin 05 canada
SamuelEcors
prazosin 1 mg capsules
DavisIgnip
pharmacy websites
ZakLit
tadalafil otc india
JackLit
cymbalta price australia
DarrylBox
atenolol cost
JosephKip
prazosin online
SamuelEcors
canadian pharmacies not requiring prescription
DarrylBox
top online pharmacy 247
DarrylBox
phenergan over the counter australia
SueLit
where can i buy bactrim
Davidevelf
keflex 1000 mg
Michaelsmopy
best online foreign pharmacy
SamuelEcors
buying diflucan
Davidevelf
drug finasteride
Davidevelf
keflex pill price
Michaelsmopy
legitimate online pharmacy uk
SamLit
synthroid 75 mcg cheap no prescription
DavisIgnip
synthroid 175 mcg tablet
SamLit
how much is generic plavix
SamuelEcors
bactrim ds
SamLit
zithromax 1000 mg pills
Michaelsmopy
voltaren 50 mg
ZakLit
clopidogrel 75 mg canada
DavisIgnip
synthroid 0.50 mg
Richardatone
how much is generic strattera
JackLit
amoxicillin azithromycin
Elwoodexcix
price of amoxicillin 875 mg
Michaelsmopy
lyrica 2019 coupon
Davidevelf
amoxicillin 500 prescription
MaryLit
bactrim ds tablets online
DavisIgnip
prednisone 15 mg tablet
SamLit
doxycycline
SamLit
generic doxycycline 3626
JackLit
cheap strattera online
DarrylBox
hydroxychloroquine order online
Williamjotly
lyrica medicine generic
Davidevelf
buy prednisone without rx
MaryLit
phenergan 25mg
IvyLit
augmentin price south africa
AlanLit
generic bactrim
MaryLit
retino 0.025 gel
JosephKip
zestril 10 mg in india
MaryLit
lyrica without rx
DavisIgnip
where to buy synthroid online
ZakLit
how to get propecia
Elwoodexcix
prednisone 5
Elwoodexcix
lisinopril 20 mg best price
CurtisHed
lisinopril 20mg coupon
SueLit
how to buy propranolol online
Elwoodexcix
diflucan buy nz
Elwoodexcix
otc albuterol usa
JosephKip
retino 0.05 price
SamuelEcors
diflucan 150 mg price
Elwoodexcix
lyrica without a prescription
JackLit
albuterol in mexico
AshLit
propranolol 20 mg price
JackLit
isotretinoin accutane
DarrylBox
buy doxycycline us