当前位置:网站首页>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边栏推荐
- leetcode 968.监控二叉树
- 基于SSM的图书馆管理系统,高质量毕业论文范例(可直接使用),项目导入视频,附送源码和数据库脚本,论文撰写教程
- RobotFramework二次开发——Socket推送实时日志
- SAP SPRO configure how to display the corresponding t-code
- Using Sqlalchemy for combined paging queries
- 在C#开发中使用第三方组件LambdaParser、DynamicExpresso、Z.Expressions,实现动态解析/求值字符串表达式
- JAXB element details
- RCE&代码执行漏洞
- 268. Missing Number
- 基于can总线的A2L文件解析(1)
猜你喜欢

SAP-ABAP-BAPI_ GOODSMVT_ How to assign values when creating a material voucher Bapi

The Chinese display of SAP client is garbled

leetcode 1579. 保证图可完全遍历

310. Minimum Height Trees

CVPR 2022 | 针对场景文本检测的视觉语言模型预训练

File download vulnerability & file read vulnerability & file delete vulnerability

leetcode 99.恢复二叉搜索树

Application of motion capture system in positioning and mapping of mobile robot in underground tunnel

Jushan database won two honors of China's information innovation industry in 2022 by AI media consulting

130. Surrounded Regions
随机推荐
Jushan database won two honors of China's information innovation industry in 2022 by AI media consulting
postgis 通过 st_dwithin 检索一定距离范围内的 元素
Leetcode 297 match de la semaine
Secondary development of robotframework -- file parsing
342. Power of Four
240. Search a 2D Matrix II
leetcode 854. 相似度为 K 的字符串
Under Xinchuang: when the domestic database stars shine
Sap-abap- how to find a table and what related tables the fields have
6月《中国数据库行业分析报告》发布!智能风起,列存更生
建立自己的网站(5)
MySQL_ Query of data processing
MySQL notes
Precautions for wechat official account development
PHP反序列化&魔术方法
Gradle notes
go语言笔记
Help financial informatization innovation, Jushan database has won more than 50 financial customers recently
从零开始写一个契约测试工具——数据库设计
leetcode 829. 连续整数求和