当前位置:网站首页>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边栏推荐
- SNC processing failed SAP Router证书重新生成
- 别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
- Universaldependencies dependency label interpretation
- MySQL 5.7 + Navicat 下载安装教程(附安装包)
- MySQL_ Create and manage tables
- Arcpy 添加图层到地图文档
- redis修改密码,及启动、查看等操作
- How to improve customer conversion rate on the official website
- RobotFramework二次开发——Socket推送实时日志
- Testing methodology - data driven testing
猜你喜欢

redis主备配置dockercompose版

leetcode 968.监控二叉树

文件下载漏洞&文件读取漏洞&文件删除漏洞

Universaldependencies dependency label interpretation

Windows下MySQL 8.0.29的详细安装教程,解决找不到VCRUNTIME140_1.dll、plugin caching_sha2_password could not be loaded

MySQL notes

重磅直播|BizDevOps:数字化转型浪潮下的技术破局之路

Tianyi cloud digital government smart data center has passed the certification

动作捕捉系统用于地下隧道移动机器人定位与建图

leetcode 834. 树中距离之和
随机推荐
2022-6-21os review group linking method
MySQL_创建和管理表
310. Minimum Height Trees
Secondary development of robotframework -- file parsing
SAP SPRO configure how to display the corresponding t-code
阿里云磁盘性能分析
2017 annual summary
RF5.0新内容速看
SAP 开发Keys 申请SSCR Keys申请
卸载MySQL 8
leetcode LCP 10. 二叉树任务调度
318. Maximum Product of Word Lengths
Sap-abap-se14 how to recover lost data
6月《中国数据库行业分析报告》发布!智能风起,列存更生
Tianyi cloud explores a new idea of cloud native and edge computing integration
[QT] QT get standard system path
190. Reverse Bits
微信支付二维码生成
Jushan database won two honors of China's information innovation industry in 2022 by AI media consulting
AcWing第54场周赛