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 ...
随机推荐
- Python3多线程之间的执行顺序问题
[本文出自天外归云的博客园] 一个多线程的题:定义三个线程ID分别为ABC,每个线程打印10遍自己的线程ID,按ABCABC……的顺序进行打印输出. 我的解法: from threading impo ...
- Git关于pull,commit,push的总结
以前总是由于自己的自身的原因,对于每一次的git的操作,我都是通过eclipse或者是idea来进行的,但是 我每一次都不是很清楚的关于这些方面的操作,现在我们来进行关于git bash的操作,正是由 ...
- SQL 逗号分隔将一行拆成多行
and number<=len(a.KOrderID) and type=)=',')
- Swing中支持自动换行的WrapLayout
http://www.cnblogs.com/TLightSky/p/3482454.html ———————————————————————————————————————————————————— ...
- Java知多少(103)网络编程之IP地址和InetAddress类
Java语言的优势之一是Java程序能访问网络资源.Java提供一系列的类支持Java程序访问网络资源. TCP/IP协议和IP地址 为了进行网络通信,通信双方必须遵守通信协议.目前最广泛使用的是TC ...
- (个人)Zookeeper集群环境部署
一.准备工作 1. 下载zookeeper,下载地址:https://zookeeper.apache.org/releases.html#download 2. 下载CentOS7的镜像,下载地址 ...
- Asp.net常用的三十多个代码(非常实用)
1.//弹出对话框.点击转向指定页面 Response.Write("<script>window.alert('该会员没有提交申请,请重新提交!')</script> ...
- [PyData] 02 - Data Preprocessing and Cleaning
From: DBWangGroup 基于该系列代码的实践与补充思考. 补充:特征工程 http://www.cnblogs.com/jasonfreak/category/823064.html & ...
- [React] 01 - Intro: javaScript library for building user interfaces
教学视频: http://www.php.cn/code/8217.html React 教程: http://www.runoob.com/react/react-tutorial.html 本篇是 ...
- jstat 简介
1. jstat -gc pid 可以显示gc的信息,查看gc的次数,及时间. 其中最后五项,分别是young gc的次数,young gc的时间,full gc的次数,full gc的时间,gc ...