【LeetCode】Hamming Distance
问题网址 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的更多相关文章
- 【leetcode】Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- 【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 ...
- 【leetcode】Edit Distance (hard)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 【LeetCode】位运算 bit manipulation(共32题)
[78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- 【LeetCode】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
- 【LeetCode】BFS(共43题)
[101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...
随机推荐
- archlinux中c语言的rpc编程
参考:http://www.cs.rutgers.edu/~pxk/rutgers/notes/rpc/ 启动rpc服务端会出现 unable to register 这个错误,据说是要启用 port ...
- css浮动与绝对定位小记
浮动 float属性可以设置的值为none,left,right.对于设置了浮动的元素,会向其父元素的左侧或右侧紧靠,默认情况下,盒子的宽度不再伸展,而是根据盒子里面的内容来确定.浮动可以让一个元素移 ...
- IntelliJ IDEA 12.0 +Maven 初使用 - WEB项目的建立
新建项目 1.Fiew菜单 - New Project,选择Maven Module项,输入Project name,选择项目所在的路径,选择SDK,其余默认即可,点击Next按钮
- How to convert webp to png/jpg/gif in MacOS
Environment I'm using OS X 10.11.4 and have homebrew 1.0.5 installed. Introduction I recently downlo ...
- c# winform DirectX播放器 可以任意设置宽高比 屏幕拉伸
第一步:dll引用 Microsoft.DirectX.dll Microsoft.DirectX.AudioVideoPlayback.dll 如果没有的话,可能需要安装微软的DRECTX JDK ...
- java主函数的含义
下面对java中的主函数进行简单的解释,解决可能困惑大家的问题,下面举的例子在实际开发中几乎不会出现,但是为了解决好奇心,大家可以这么去尝试一下! 我们在java中看到的主函数通常是这样的:publi ...
- TTradmin v1.1 - 免端口映射穿透任何内网、基于radmin核心的即时远程协助
TTradmin 是一款免端口映射可直接穿透任何内网,基于radmin核心的即时远程协助软件.在使用的时候只需要保证“协助端”和“被协助端”使用同一个验证码即可实现安全便捷的远程控制,不需要进 ...
- 伪共享和缓存行填充,从Java 6, Java 7 到Java 8
关于伪共享的文章已经很多了,对于多线程编程来说,特别是多线程处理列表和数组的时候,要非常注意伪共享的问题.否则不仅无法发挥多线程的优势,还可能比单线程性能还差.随着JAVA版本的更新,再各个版本上减少 ...
- 用java实现文件下载,提示java.lang.IllegalStateException: getOutputStream() has already been called for this response
1. 用java实现文件下载,提示java.lang.IllegalStateException: getOutputStream() has already been called for this ...
- repeater留言板[转]
做了一个网站,其中的在线留言板块是用Repeater来显示留言的,这样可以用少的代码还实现多的功能,但是不知道怎么分页,要是留言过多就会使页面变的很长,能过查看众多网友的经验,知道用PagedData ...