标签:math maxlength 距离 print turn 进制 line title span
public class Solution {
public int HammingDistance(int x, int y) {
int distance = 0;
string sX = Convert.ToString(x, 2);
string sY = Convert.ToString(y, 2);
int maxLength = Math.Max(sX.Length, sY.Length);
//填充0,使两个字符串右对齐
sX = sX.PadLeft(maxLength, ‘0‘);
sY = sY.PadLeft(maxLength, ‘0‘);
for (int i = 0; i < maxLength; i++) {
if (sX[i] != sY[i]) {
distance++;
}
}
return distance;
}
}
461.求两个数字转成二进制后的“汉明距离” Hamming Distance
标签:math maxlength 距离 print turn 进制 line title span
原文地址:http://www.cnblogs.com/xiejunzhao/p/8ad08bda61864780eefddb3b70139cf8.html