问题网址 https://leetcode.com/problems/hamming-distance/

就是一个异或后,求1的位数的问题。

看到问题之后,首先困扰是: int能不能求异或?是不是要转成二进制才可以?

答案是肯定的。直接 x^y 就行了。

然后就是统计位数的问题了

答案一:https://discuss.leetcode.com/topic/72636/c-simple-solution-0ms

liupeng的答案

int hammingDistance(int x, int y) {

    int tmpInt=x^y;
int dis=; while(tmpInt)
{
if((tmpInt>>)<< != tmpInt)
      //tmpInt右移一位,再左移动一位,相当于最后一位设为0;如果等于原数,就证明原来最后一位就是0.反之,是1
      // 很巧妙
{
++dis;
} tmpInt>>=;
} return dis;
}

后话

如果真的要转成二进制,可以转成字符串

char str[10];
itoa(tmpInt, str, 2);

这个2 可以自由发挥了。别的进制也可以。

【LeetCode】Hamming Distance的更多相关文章

  1. 【leetcode】Edit Distance

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  2. 【leetcode】1184. Distance Between Bus Stops

    题目如下: A bus has n stops numbered from 0 to n - 1 that form a circle. We know the distance between al ...

  3. 【leetcode】Edit Distance (hard)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  4. 【LeetCode】位运算 bit manipulation(共32题)

    [78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...

  5. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  6. 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

  8. 【LeetCode】哈希表 hash_table(共88题)

    [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...

  9. 【LeetCode】BFS(共43题)

    [101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...

随机推荐

  1. 夺命雷公狗-----React---24--小案例之react经典案例todos(单条任务的删除)

    我们的组建分析图 我们组建需要的是删除,数据流方式如下所示: 为了更方便下一步操作,先写个函数他 然后在Ul组建里面对她进行处理 然后在Zong组建里对数据进行处理,如下所示: 但是悲剧的一幕出现了, ...

  2. cento下安装elasticsearch2.4.2小记

    1.首先需要安装好java,并配置好环境变量 2.下载elasticsearch的rpm包,命令如下: wget https://download.elastic.co/elasticsearch/r ...

  3. html5,加密元素

    <form action="">    账号:<input type="text" name="user">     ...

  4. jquery 获取父窗口的元素 父窗口 子窗口

    一.获取页面元素 取父窗口的元素方法:$(selector, window.parent.document); 那么你取父窗口的父窗口的元素就可以用:$(selector, window.parent ...

  5. android使用ksoap2调用sap的webservice

    public void on_clicked(View view) { Thread webserviceThread = new Thread() { public void run() { Str ...

  6. Java连接SqlServer2008数据库(转)

    Java连接SqlServer2008数据库 首先下载JDBC:下载地址:http://www.microsoft.com/zh-cn/download/details.aspx?id=21599 下 ...

  7. BigDecimal使用中的坑

    1.BigeDecimal调用divide时一定要记得规定小数位数的保留情况,不然除不尽的时候报错. 2.使用该种BigeDecimal时,加减乘除都要使用它内部封装好的方法,不然容易报错.

  8. linux思维导图

  9. pt-online-schema-change原理解析

    都说pt-toolkit工具集中的pt-online-schema-change可以在线不锁表修改表结构,那么这个工具具体是什么原理呢,请见下面娓娓道来: 1.pt-online-schema-cha ...

  10. jquery与自己写的js文件冲突解决办法

    先加载JQUERY,然后使用语句  jQuery.noConflict(); 再加载其他JS文件,后面在使用jQuery时都换下,如:$('#div') 换成 jQuery('#div'), 如果嫌j ...