问题网址 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. mysql 卸载 linux

    root@localhost ~]# rpm -qa | grep -i mysqlMySQL-client-5.5.52-1.linux2.6.x86_64MySQL-server-5.5.52-1 ...

  2. 走进spring之springmvc实战篇(一)

    本篇运用springmvc来试着写一个登录注册页面 在动手之前,我们需要了解下springnvc.这里先献上一张springmvc的流程图及讲解. Spring的MVC框架是一个基于Dispatche ...

  3. SQL行转列+动态拼接SQL

    数据源       Name AreaName qty Specific 叶玲 1 60 1 叶玲 2 1 1 叶玲 6 1 0 叶玲 7 5 0 叶玲 8 1 1 显示效果: Name 1 2 8 ...

  4. CSS3 笔记五(Buttons)

    Some examples 1> Input width animation Search: Code <!DOCTYPE html> <html> <head&g ...

  5. 07-阻塞赋值与非阻塞赋值原理分析——小梅哥FPGA设计思想与验证方法视频教程配套文档

    芯航线--普利斯队长精心奉献   实验目的:掌握阻塞赋值与非阻塞赋值的区别 实验平台:无 实验原理:     阻塞赋值,操作符为"=","阻塞"是指在进程语句( ...

  6. MySQL字符集转换(latin1到utf8)

    http://blog.chinaunix.net/uid-25266990-id-3344584.html

  7. jQuery基本操作

    jQuery简介 jQuery是一个兼容多浏览器的javascript库,极大地简化了 JavaScript 编程,核心理念是write less,do more(写得更少,做得更多),对javasc ...

  8. winfrom 文字滚动

    winfrom 文字滚动 http://www.codeproject.com/Articles/6913/Creating-a-professional-looking-GDI-drawn-cust ...

  9. set、def、lambda、内置函数、文件操作

    set : 无序,不重复,可以嵌套 .add (添加元素) .update(接收可迭代对象)---等于批量 添加 .diffrents()两个集合不同差 .sysmmetric difference( ...

  10. VS2012一打开就停止工作的解决方法

    昨天刚装好VS2012,用得好好的,嘿,今早儿 一打开程序,就告诉我 VS2012停止工作,只有关闭程序或者调试程序. 小主酸菜郁闷不已,必须找到解决方法啊,错误提示如图: 错误原因:        ...