461. Hamming Distance
https://leetcode.com/problems/hamming-distance/
将两个二进制数比较,输出不同位数的个数
Input: x = 1, y = 4 Output: 2 Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑ 解题思路:
此题考察的是知识点是运算符,特别是异或运算符^
异或运算符作用为“相同出0,不同出1”,这个特性正好可以用来解题
那么正常的思路就是将x,y两数进行异或运算,然后统计1的出现次数 解法(Python):
class Solution(object):
def hammingDistance(self, x, y):
return bin(x^y).count("");
这个解法其实很low的,首先用了bin()函数,作用是将异或结果二进制化为字符串,然后利用字符串函数count统计1出现的次数并输出
优秀解法评析(Java):
public int hammingDistance(int x, int y) {
int xor = x ^ y, count = 0;
for (int i=0;i<32;i++) count += (xor >> i) & 1;
return count;
}
由于题目有限制0 ≤ x
, y
< 231
所以这个解法将异或结果一个个移位,然后和1和运算,自然相同为1,不同为0,然后用count器加上这个相同的1自然就是“1出现的次数”
461. Hamming Distance的更多相关文章
- LeetCode:461. Hamming Distance
package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...
- Leetcode#461. Hamming Distance(汉明距离)
题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...
- 【leetcode】461. Hamming Distance
problem 461. Hamming Distance solution1: 根据题意,所求汉明距离指的是两个数字的二进制对应位不同的个数.对应位异或操作为1的累积和. class Solutio ...
- [Leetcode/Javascript] 461.Hamming Distance
[Leetcode/Javascript] 461.Hamming Distance 题目 The Hamming distance between two integers is the numbe ...
- 461. Hamming Distance and 477. Total Hamming Distance in Python
题目: The Hamming distance between two integers is the number of positions at which the corresponding ...
- 4. leetcode 461. Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- 461. Hamming Distance(leetcode)
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 解题报告
题目要求 The Hamming distance between two integers is the number of positions at which the corresponding ...
- [LeetCode&Python] Problem 461. Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
随机推荐
- sql server 中隐藏掉无关数据库
先贴上我实际测试的效果 方法一: Problem I have a SQL Server instance that has hundreds of databases. Navigating th ...
- python学习之day6,常用标准模块
1.时间模块 time import time #时间戳转字符串格式 a = time.time() print(a) #打印时间戳 b = time.localtime(a) #把时间戳转换成时间对 ...
- iOS推送流程
1. 在apple开发者帐号上创建一个BundleID,创建证书或者Xcode上都是用这个BundleID(例如com.mycompany.pushDemo) 2. 代码层面: 在capability ...
- jQuery 树形菜单
树形菜单 在 jQuery easyu中其左侧的主菜单使用的是 easyui 中的 tree 组件,不是太熟悉,不过感觉不是太好用. 比如 easyui 中的 tree 需要单击分叉节点前的小三角,才 ...
- 使用charles V3.11.2 实现SSL抓包
首先,确认开启了SSL选项: 然后到Help->SSL Proxying里查看帮助; 根据提示,将android手机连接到局域网的wifi上,然后将wifi连接的代理设置为192.168.21. ...
- SOA 实现:服务设计原则
http://www.ibm.com/developerworks/cn/webservices/ws-soa-design/ 引言 面向服务的体系结构(Service-Oriented Archit ...
- Hibernate(Control)
案例:http://blog.csdn.net/jiuqiyuliang/article/details/39380465 对象关系映射框架,它对JDBC进行了轻量级的对象封装,可以使用对象编程思维来 ...
- jdk自带的jvm监测程序
jinfo:可以输出并修改运行时的java 进程的opts. jps:与unix上的ps类似,用来显示本地的java进程,可以查看本地运行着几个java程序,并显示他们的进程号. jstat:一个极强 ...
- Php compiler for .NET framework
https://phalanger.codeplex.com http://tomasp.net/blog/ducktyping-in-phalaner.aspx/ https://visualstu ...
- TTTAttributedLabel xib sb lineSpacing not working
https://github.com/TTTAttributedLabel/TTTAttributedLabel/issues/733 set the same text in storyboard ...