LeetCode Hamming Distance
原题链接在这里:https://leetcode.com/problems/hamming-distance/
题目:
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x
and y
, calculate the Hamming distance.
Note:
0 ≤ x
, y
< 231.
Example:
Input: x = 1, y = 4 Output: 2 Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑ The above arrows point to positions where the corresponding bits are different.
题解:
Bits different自然而然就想到了xor.
利用Number of 1 Bits的数xor 结果的bit 1个数.
或者用Integer.bitCount() function.
Time Complexity: O(1). Space: O(1).
AC Java:
public class Solution {
public int hammingDistance(int x, int y) {
return Integer.bitCount(x^y);
}
}
LeetCode Hamming Distance的更多相关文章
- LeetCode——Hamming Distance
LeetCode--Hamming Distance Question The Hamming distance between two integers is the number of posit ...
- [LeetCode] Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- LeetCode Total Hamming Distance
原题链接在这里:https://leetcode.com/problems/total-hamming-distance/ 题目: The Hamming distance between two i ...
- LeetCode 461. Hamming Distance (汉明距离)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [Leetcode/Javascript] 461.Hamming Distance
[Leetcode/Javascript] 461.Hamming Distance 题目 The Hamming distance between two integers is the numbe ...
- [LeetCode] 477. Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] 461. Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- 【LeetCode】461. Hamming Distance 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 方法一:异或 + 字符串分割 方法二: ...
随机推荐
- 51nod1088(最长回文子串)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1088 题意: 中文题目诶~ 思路: 这道题字符串长度限定为1 ...
- Power BI for Office 365(五)Power View第二部分
继续上一篇Power View 报表的创建, Anna觉得为每一个大类创建一张单独的报表似乎很不现实,所以她决定按照另外一种方式来设计报表,从而满足最终用户想要查看不同大类下的数据的要求. 于是Ann ...
- WPF中Ribbon控件的使用
这篇博客将分享如何在WPF程序中使用Ribbon控件.Ribbon可以很大的提高软件的便捷性. 上面截图使Outlook 2010的界面,在Home标签页中,将所属的Menu都平铺的布局,非常容易的可 ...
- 【bzoj1499】瑰丽华尔兹
这名字听起来实在有点耳熟..? 好吧去年暑假就应该过的题...切了 先注意到,天使施魔法的次数不限:我们可以使得某个时刻在特定方向移动一段距离(最长的长度为那个时间段)然后怎么来考虑这个DP: T,X ...
- Codeforces525E Anya and Cubes(双向搜索)
题目 Source http://codeforces.com/contest/525/problem/E Description Anya loves to fold and stick. Toda ...
- HDU5716 : 带可选字符的多字符串匹配
shift-and算法,设$v[i][j]$表示文本串长度为$i$的前缀能否匹配模式串长度为$j$的前缀,$f[i][j]$表示字符$i$能否匹配模式串的第$j$个位置,那么有$v[i+1][j+1] ...
- CSS3 动画
通过 CSS3,我们能够创建动画,这可以在许多网页中取代动画图片.Flash 动画以及 JavaScript. CSS3 动画 CSS3 @keyframes 规则 如需在 CSS3 中创建动画, ...
- iOS二维码生成-libqrencode编译报错
libqrencode使用 1.将libqrencode文件夹整个拖入项目文件夹中 2.在要生成二维码的页面的 .m文件头部添加 #import "QRCodeGenerator.h&quo ...
- word-wrap: break-word;和word-break: break-all;的区别
详细查看以下链接.(转载自张鑫旭大神空间) http://www.zhangxinxu.com/wordpress/2015/11/diff-word-break-break-all-word-wra ...
- 对拍老是忘记的看这里:bat代码
需要写三个程序,makedata.exe 产生测试数据, program1.exe 是你要检测的程序,program2.exe 往往是一个正确但效率不高(暴力的居多)的程序. 代码很简单,稍作解释:l ...