public class Solution
{
public int HammingDistance(int x, int y)
{
int[] aryA = new int[];
int[] aryB = new int[]; int i = ;
int j = ; do
{
aryA[i] = x % ;//将10进制转换为2进制
x = x / ;
i++;
}
while (x != ); do
{
aryB[j] = y % ;//将10进制转换为2进制
y = y / ;
j++;
}
while (y != ); int result = ;
for (int k = ; k < ; k++)
{
if (aryA[k] != aryB[k])//查找对应的二进制位,如果一个是0一个是1
{
result++;
}
}
//Console.WriteLine(result);
return result;
}
}

https://leetcode.com/problems/hamming-distance/#/description

将10进制转为2进制

补充一个python的实现,使用位操作:

 class Solution:
def hammingDistance(self, x: int, y: int) -> int:
z = x ^ y
cnt =
mask =
for i in range():
t = z & mask
mask <<=
if t != :
cnt +=
return cnt

leetcode461的更多相关文章

  1. [Swift]LeetCode461. 汉明距离 | Hamming Distance

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

随机推荐

  1. CMD命令,动态执行存储或DML命令

    --exec master..xp_cmdshell CMD命令 --EXECUTE sys.sp_sqlexec 执行存储 --EXEC sp_executesql 执行DML语句

  2. 周强201771010141《面向对象程序设计(java)》第六周学习总结

    枚举是一种特殊的数据类型,之所以特殊是因为它既是一种类(class)类型却又比类型多了些特殊的约束,但是这些约束的存在也造就了枚举类型的简洁,安全性以及便捷性.创建枚举类型要使用enum关键字,隐含了 ...

  3. [转] C++ 和 python之间的互相调用

    转载自:https://www.cnblogs.com/apexchu/p/5015961.html 一.Python调用C/C++ 1.Python调用C动态链接库 Python调用C库比较简单,不 ...

  4. controller向layout传值

    Yii2,layout中使用Controller的值,Controller向layout传值的两种方式. yii2中在通过Controller向layout中传值,layout中访问Controlle ...

  5. Mac + PyCharm 安装 Opencv3 + python2.7

    本文地址:http://www.cnblogs.com/QingHuan/p/7354074.html 转载请注明本文地址,方便读者查看本文更新,谢谢! 今天要在Mac上安装OpenCV,过程非常曲折 ...

  6. 如何用Caffe训练自己的网络-探索与试验

    现在一直都是用Caffe在跑别人写好的网络,如何运行自定义的网络和图片,是接下来要学习的一点. 1. 使用Caffe中自带的网络模型来运行自己的数据集 参考 [1] :http://www.cnblo ...

  7. mysql连接拍错总结

    1. ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10060)   ? 关闭网络防火墙.

  8. Announcing the Operate Preview Release: Monitoring and Managing Cross-Microservice Workflows

    转自:https://zeebe.io/blog/2019/04/announcing-operate-visibility-and-problem-solving/   Written by Mik ...

  9. 让docker容器开机启动

    网上有些文章说,要让docker 的容器自动在开机启动,是写脚本,比如在 rc.local 中写.其实完全没必要这么麻烦,docker 有相关指令,docker run 指令中加入 --restart ...

  10. nginx upstream轮询配置

    nginx upstream nginx的upstream官方地址为:http://nginx.org/cn/docs/http/ngx_http_upstream_module.html 轮询分为多 ...