【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. // 这个题目的重点是 比较 ...
随机推荐
- 细数那些我们都习惯了的Java谣言
我是一个Java的反对者,至于为什么,我想最大的一个原因是它不实在,不管是当年sun所说的一些言论,还是如今Java用户的一些言论,都有蛊惑之嫌,甚至很多太假了,而这些言论层出不穷,其实就语言本身我不 ...
- 【unity3d游戏开发脚本笔记之一:坐标系选择对物体运动的影响】
时间:2016年9月24日17:38:21 作者:yexiaopeng 博客园 在unity3d的世界中,其坐标系可分为四种,世界坐标系-WorldSpace 本地坐标系-LocalS ...
- URL请求工具
工作中有个需求,定期请求多个URL.“定期”采用计划任务实现,请求URL,虽说start url可以实现,但不灵活.自己制作了个专门请求URL的工具,并记录请求结果. 控制台程序代码: class P ...
- Android 开发命令行完全攻略
作为命令行的爱好者,我想写这个主题已经有好一段时间了.除了显得很酷之外,命令行的使用能够提高我们的开发效率,因为相比通过鼠标点击一系列的菜单选项,使用键盘输入几个字符并点击 TAB 健显然会快很多. ...
- 阿里云CDN刷新预热接口
阿里云OSS映射的文件地址需要即时访问到最新数据,需要即时调用CDN的刷新预热类接口 RefreshObjectCaches 刷新接口. 参考官方接口文档资料:https://help.aliyun. ...
- session原理及实现共享
一.session的本质http协议是无状态的,即你连续访问某个网页100次和访问1次对服务器来说是没有区别对待的,因为它记不住你.那么,在一些场合,确实需要服务器记住当前用户怎么办?比如用户登录邮箱 ...
- Spring之ClassPathResource加载资源文件
先看Demo: 1 @Test 2 public void testClassPathResource() throws IOException { 3 Resource res = new Clas ...
- [已解决] git 重命名文件夹
git mv oldfolder newfolder 原文地址:http://www.cnblogs.com/gifisan/p/5980608.html
- 【转】缺少servlet-api.jar包
转载地址:http://blog.sina.com.cn/s/blog_6cfb18070100n7pu.html 在Eclipse中缺省servlet-api.jar包,由于servlet-apbi ...
- CentOS 6.6安装配置LAMP服务器(Apache+PHP5+MySQL)
准备篇: CentOS 6.6系统安装配置图解教程 http://www.osyunwei.com/archives/8398.html 1.配置防火墙,开启80端口.3306端口 vi /etc/s ...