【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. // 这个题目的重点是 比较 ...
随机推荐
- iOS版微信朋友圈数据库的简要分析
本文版权归cxun所有,如有转载请注明出处与本文链接,谢谢!原文地址:http://www.cnblogs.com/cxun/p/4550523.html 之前写了一些关于微信聊天记录的博文之后,不少 ...
- Core 1.0中publishOptions Include的bug
"publishOptions": { "include": [ "wwwroot", "Views", "A ...
- Yii2中多表关联查询(join、joinwith)
我们用实例来说明这一部分 表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer (id customer_name) 订单表Order (id order_name ...
- Server.MapPath和Request.PhysicalApplicationPath的异同
很多人对它们都不陌生,在众多的WEB程序中,使用Server.MapPath和Request.PhysicalApplicationPath来操作目录/文件的几率参半,我曾经也经常混用,然而时间久了. ...
- 1016. Boundaries on A New Kind of Science 解题报告
题目链接: http://soj.sysu.edu.cn/1016 题目大意: 给定一个字符串和256条规则,将某条规则应用于字符串,字符串将发生变化,给定一个数max,求出在max步内可以将字符串变 ...
- PowerShell 连接SQL
因为对SQL操作比较多,但有些操作其实都是重复性的,只是参数不太一样了,例如silo id, server name 等.希望可以通过powershell脚本提高效率. 尝试如下 1. 使用Power ...
- centos 配置 ssl服务
使用的是appach 2.4.10 版本 各个版本配置不同 1.首先修改httpd.conf 文件 appach 安装目录下的 conf文件夹中找到 #LoadModule socache_shmcb ...
- CSS学习之道
@media的IE-hacks,精彩绝伦 http://blog.keithclark.co.uk/moving-ie-specific-css-into-media-blocks/
- Razor语法&ActionResult&MVC
Razor代码复用 mvc 4 razor语法讲解和使用 了解ASP.NET MVC几种ActionResult的本质:EmptyResult & ContentResult 了解ASP.NE ...
- linux 中更改用户权限和用户组的命令chmod,chgrp实例
linux 中更改用户权限和用户组的命令实例; 增加权限给当前用户 chmod +wx filename chmod -R 777 /upload 用户组 chgrp -R foldname zdz ...