LeetCode 461 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.
题目分析及思路
题目给出两个整数,要求返回它们之间的Hamming distance。Hamming distance指的是两个整数对应的二进制的对应位上数字不同的个数。可以先对两个整数做异或操作,然后对结果的每一位和1做与操作,若等于1就加一,否则就跳过。
python代码
class Solution:
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
bits = x ^ y
count = 0
for i in range(0,32):
if bits & 1 != 0:
count = count + 1
bits = bits >> 1
return count
LeetCode 461 Hamming Distance 解题报告的更多相关文章
- 【LeetCode】461. Hamming Distance 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 方法一:异或 + 字符串分割 方法二: ...
- 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】477. Total Hamming Distance 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...
- 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 bits ...
- 4. leetcode 461. Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- LeetCode 461. Hamming Distance (C++)
题目: The Hamming distance between two integers is the number of positions at which the corresponding ...
- [LeetCode] 461. Hamming Distance(位操作)
传送门 Description The Hamming distance between two integers is the number of positions at which the co ...
随机推荐
- Git关于pull,commit,push的总结
以前总是由于自己的自身的原因,对于每一次的git的操作,我都是通过eclipse或者是idea来进行的,但是 我每一次都不是很清楚的关于这些方面的操作,现在我们来进行关于git bash的操作,正是由 ...
- 【iCore4 双核心板_ARM】例程二十三:LWIP_HTTP实验——网页服务器
实验现象: 核心代码: int main(void) { system_clock.initialize(); led.initialize(); adc.initialize(); delay.in ...
- 前端分页插件pagination
摘要: 最近在开发项目中又用到了前端分页,以前也做过,为了方便以后使用所以将他封装成第三方插件,不依赖任何库.网上已经有很多插件,问什么还要自己造轮子? 自己写的扩展性高 不依赖任何库 作为一次技术沉 ...
- Zookeeper安装使用及JavaAPI使用
一.Zookeeper单击模式安装及使用 1.系统环境 2.导入JDK和Zookeeper包 1).使用SecureCRT工具打开SFTP连接,直接拖拽,到当前用户文件夹下,然后使用mv命令(mv 文 ...
- Linux设备驱动剖析之IIC(二)
953行,适配器的编号大于MAX_ID_MASK是不行的,MAX_ID_MASK是一个宏,展开后的值为61. 957至968行,关于管理小整形ID数的,没怎么了解,略过. 974行,调用i2c_reg ...
- mysql 冷热备份
文章转自:http://www.linuxidc.com/Linux/2014-03/98710.htm 冷备份(OFF, 慢, 时间点上恢复)冷备份发生在数据库已经正常关闭的情况下,当正常关闭时会提 ...
- Unity3D Shader水波效果
水波效果 Shader "Custom/WaterWave" { Properties { _MainTex ("Base (RGB)", 2D) = &quo ...
- Fiddler如何捕捉DefaultHttpClient的HTTP请求
实际开发中为了解决Ajax跨域请求问题,会通过一个同域的控制器在服务端代理请求目标的Web API并将结果返回.为了便于调试这类请求,我们希望通过Fiddler可以监控到这些请求.Fiddler官方给 ...
- mui+回复弹出软键盘
最近再做一个APP的时候,有一个评论回复的功能,在做APP的时候,直接用手指触发focus事件,来唤醒软键盘的弹出没有问题, 但是现在的功能需要对点击回复进行弹出软键盘来操作,参考过很多都有问题,后来 ...
- 3.STM32F4按键扫描函数
//按键处理函数 //返回按键值 //mode:0,不支持连续按;1,支持连续按; //0,没有任何按键按下 //1, KEY0 按下 2, KEY1 按下 3, KEY2 按下 4, WKUP 按下 ...