当前位置:网站首页>461. Hamming Distance
461. Hamming Distance
2022-06-22 13:15:00 【Sterben_ Da】
461. Hamming Distance
Easy
3087198Add to ListShare
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x and y, return the Hamming distance between them.
Example 1:
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.
Example 2:
Input: x = 3, y = 1 Output: 1
Constraints:
0 <= x, y <= 231 - 1
class Solution:
def hammingDistance(self, x: int, y: int) -> int:
"""
assert Solution().hammingDistance(1, 4) == 2
assert Solution().hammingDistance(3, 1) == 1
assert Solution().hammingDistance(37, 8) == 4
Find the number of unequal binary bits in the same position
"""
# result = 0
# while x > 0 or y > 0:
# if x % 2 != y % 2:
# result += 1
# x >>= 1
# y >>= 1
result = bin(x ^ y).count('1')
return result边栏推荐
猜你喜欢

SAP 系统License查看申请及导入

这不会又是一个Go的BUG吧?

建立自己的网站(5)

Tis tutorial 04 client

Sap-mm-migo 311 intra factory transfer inventory

leetcode 第 297 场周赛

SNC processing failed SAP Router证书重新生成

Reddit product director: a practical guide for NFT members for Web3 creators

130. Surrounded Regions

Help financial informatization innovation, Jushan database has won more than 50 financial customers recently
随机推荐
RobotFramework二次开发——实时日志
天坑专业学IC设计自学的话有公司会要吗
RCE&代码执行漏洞
vs code
MySQL_数据处理之增删改
RobotFramework二次开发——Socket推送实时日志
693. Binary Number with Alternating Bits
Help financial informatization innovation, Jushan database has won more than 50 financial customers recently
[QT] qfileinfo get the components of the file name
redis主备配置dockercompose版
Hurun Research Institute launched the list of potential enterprises of China's meta universe, and Jushan database was selected as the future star enterprise
Recommend a virtual machine software for fast cluster building of M1 chip computers
Sap-mm-migo 311 intra factory transfer inventory
leetcode 1130. 叶值的最小代价生成树
Secondary development of robotframework -- socket push real-time log
think php环境搭建笔记
记录阿里云ECS实例重启之后无法登录解决方法(亲身实践)
Alicloud disk performance analysis
leetcode 85. 最大矩形
剑指 Offer II 114. 外星文字典