LeetCode-461. Hamming Distance-位运算
来源:互联网 发布:263网络会议室 编辑:程序博客网 时间:2024/05/29 10:56
题目:
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.
解答:
class Solution(object): def hammingDistance(self, x, y): """ :type x: int :type y: int :rtype: int """ return bin(x^y).count('1')
知识点:
1、bin()
bin
(x)Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int
object, it has to define an__index__()
method that returns an integer.
将整数转换为二进制字符串
https://docs.python.org/2/library/functions.html#bin
bin
(x)Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int
object, it has to define an__index__()
method that returns an integer.
将整数转换为二进制字符串
https://docs.python.org/2/library/functions.html#bin
2、位运算
http://www.runoob.com/python/python-operators.html
阅读全文
0 0
- leetcode 461.Hamming Distance(easy)[位运算]
- LeetCode-461. Hamming Distance-位运算
- LeetCode 461. Hamming Distance 知识点复习之位运算
- LeetCode(一)--461.Hamming Distance&476.Number Complement 位运算
- LeetCode 416 Hamming Distance(位运算)
- LeetCode 461. Hamming Distance
- 【Leetcode】461. Hamming Distance
- LeetCode 461. Hamming Distance
- LeetCode - 461. Hamming Distance
- LeetCode-461. Hamming Distance
- 【LeetCode】 461. Hamming Distance
- Leetcode-461. Hamming Distance
- Leetcode 461. Hamming Distance
- LeetCode--461. Hamming Distance
- 461. Hamming Distance [LeetCode]
- [LeetCode]461. Hamming Distance
- leetcode 461. Hamming Distance
- Leetcode 461. Hamming Distance
- numpy学习笔记(一)
- 2017年11月5日 第二十七次总结
- imx6q 串口设置
- python3.6 在windows 系统下安装mysqldb
- 联合体的使用
- LeetCode-461. Hamming Distance-位运算
- c语言--数组
- Ubuntu登陆后桌面图标消失,只有背景和鼠标存在
- win10安装openni1.5.4.0安装教程
- UnityWebGl那些事儿
- Hashpump实现哈希长度扩展攻击
- Spring 使用@PropertySource+Emvironment读取properties文件
- Magento中getModel与getData方法结合使用获取信息
- ExpandableListView的使用