【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. // 这个题目的重点是 比较 ...
随机推荐
- uexWeiXin插件
uexWeiXin插件 接入指引 常用功能 接入指引 Android 准备一个应包名:平台默认包名:org.zywx.wbpalmstar.widgetone.uex加上应用id AppCan平台默认 ...
- 【转】PowerShell入门(十一):编写脚本模块
转至:http://www.cnblogs.com/ceachy/archive/2013/03/08/PowerShell_Script_Module.html 现在通过编写模块就可以在PowerS ...
- 在64位SQL Server中创建Oracle的链接服务器
当我们同时使用SQL Server和Oracle来存储数据时,经常会用到跨库查询.为了方便使用跨库查询,一个最好的办法就是通过创建链接服务器来实现.既可以在SQL Server中创建Oracle的链接 ...
- 电脑安装Android4.0虚拟机的做法
在开始教程之前,先给大家展示一下成功运行Android 4.0虚拟机的界面,经过笔者测试,体验很流畅,喜欢DIY和对开发感兴趣的朋友们可以猛击下一页,继续浏览教程. 准备工作 在体验前我们首先要下载J ...
- joson返回数据库的时间格式在前台用js转换
function ChangeDateFormat(val) { if (val != null) { var date = new Date(parseInt(val.replace("/ ...
- 谷歌 HTML/CSS 规范 2016-12-30
背景 这篇文章定义了 HTML 和 CSS 的格式和代码规范,旨在提高代码质量和协作效率. 通用样式规范 协议 省略图片.样式.脚本以及其他媒体文件 URL 的协议部分(http:,https:),除 ...
- HttpClient中转上传文件
场景:客户端(浏览器)A---->选择文件上传---->服务器B---->中转文件---->服务器C---->返回结果---->服务器B---->客户端A 有 ...
- android屏幕适配原则
1.尽量使用线性布局和相对布局 2.尽量使用dip和sp,不要使用px 3.为不同的分辨率提供不同的布局文件和图片 4.在AndroidMainfest.xml中设置多分辨率支持 5.层级嵌套,合理布 ...
- 教你如何查看一款App里面所包含的图片
在开发制作App的过程中,有时候会想看看一些精美的App里面所设计的素材.这个时候就需要用到我给大家展现的方法了.下面就看看该如何操作能让一个App呈现出它原始的一面,这次我以Any.Do为例给大家演 ...
- 使用JavaScript访问子节点方法elementNode.childNodes时,需要注意的地方
有这样一个HTML结构 <div> javascript <p>javascript</p> <div>jQuery</div> <h ...