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

Given two integers x and y, calculate the Hamming distance.

Note:
0 ≤ xy < 231.

题意,两个数字转为二进制,然后异或一下,判断有多少个1

直接异或判断,简单粗暴

class Solution {private int ham(int num) {int sum = 0;while (num != 0) {if (num%2 == 1)sum ++;num = num/2;}return sum;}public int hammingDistance(int x, int y) {int res = x^y;return ham(res);}
}

转载于:https://www.cnblogs.com/Moriarty-cx/p/9769685.html

[LeetCode] 461. Hamming Distance相关推荐

  1. Leetcode 461. Hamming Distance JAVA语言

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

  2. LeetCode 461 Hamming Distance(汉明距离)

    Q:The Hamming distance between two integers is the number of positions at which the corresponding bi ...

  3. LeetCode 461. Hamming Distance

    题目: The Hamming distance between two integers is the number of positions at which the corresponding ...

  4. 【LeetCode】461. Hamming Distance (java实现)

    2019独角兽企业重金招聘Python工程师标准>>> 原题链接 https://leetcode.com/problems/hamming-distance/ 原题 The Ham ...

  5. LeetCode之Hamming Distance

    1.题目 The Hamming distance between two integers is the number of positions at which the corresponding ...

  6. 461. Hamming Distance【数学|位运算】

    2017/3/14 15:23:55 The Hamming distance between two integers is the number of positions at which the ...

  7. LeetCode之461. Hamming Distance

    ------------------------------------------------------------------ AC代码: public class Solution {publ ...

  8. LeetCode(191 461):位1的个数 Number of 1 Bits 汉明距离 Hamming Distance(Java)

    2019.9.14 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) github:https://github.com/ChopinXBP/LeetCode-Babel 之前在 ...

  9. C#LeetCode刷题之#461-汉明距离​​​​​​​(Hamming Distance)

    问题 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意:0 ≤ x, y < 231. 输入: x = 1, y = ...

最新文章

  1. 2014 WAP校园招聘笔试题
  2. 另一种方法做本地reuse library的开发
  3. 终于有人把用户画像的流程、方法讲明白了
  4. layui流加载及传参
  5. 使用gridlayout布局后,因某些原因又删除,并整理文件夹结构时,Unable to resolve target #39;android-7#39;...
  6. 【刷题记录】GCJ 2.71~2.72
  7. 捷联惯导算法 matlab,捷联惯导算法与组合导航原理资料及更正
  8. linux hping3命令,系列H - hping3 - 测试网络及主机的安全 - 《Linux命令大全》 - 技术池(jishuchi.com)...
  9. python爬iptv直播源脚本_GitHub - bensonlai2002/iptv-m3u-maker: IPTV 国内+国外 电视台直播源m3u文件, 收集汇总本地源脚本...
  10. dell h330 不要做raid5 ,做raid0就可以了
  11. 成功三步曲:有勇、有谋、有德
  12. 如何把视频压缩到最小?请看详细步骤
  13. 2013年最火和最挣钱的IT职位
  14. JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
  15. Android手机直播系统开发介绍
  16. 给tensor增加维度 或 减少维度
  17. JDK、JER、JVM三者间的联系与区别
  18. php 浪漫代码,技术宅用代码表白也可以很浪漫
  19. 微软首席数字官亲述微软自己的数字化转型故事
  20. Kafka分区副本分配规则

热门文章

  1. Centos7手动安装OpenStack Mitaka版本--KeyStone安装
  2. Java多线程系列--“JUC原子类”01之 框架
  3. Java 线程池中的线程复用是如何实现的?
  4. Flink or Spark?实时计算框架在K12场景的应用实践
  5. 一个简单案例,5 分钟看懂 Java Lamdba 表达式
  6. Java注释@interface的用法【转】
  7. 实战:Nginx集成Lua脚本并调用memcached
  8. SIP协议和压力测试, SIP callflow图形生成
  9. 39.左值、左值引用、右值、右值引用
  10. Java final、static final 讲解