当前位置:网站首页>力扣461.汉明距离
力扣461.汉明距离
2022-06-21 16:53:00 【SS_zico】
- 汉明距离
在信息论中,两个等长字符串之间的汉明距离是两个字符串对应位置的不同字符的个数。换句话说,它就是将一个字符串变换成另外一个字符串所需要替换的字符个数。
布赖恩·克尼根算法
class Solution:
def hammingDistance(self, x, y):
xor = x ^ y
distance = 0
while xor:
distance += 1
# remove the rightmost bit of '1'
xor = xor & (xor - 1)
return distance
C++
class Solution {
public:
int hammingDistance(int x, int y) {
int res = x^y;
int c = 0;
while(res)
{
if(res&1 == 1)
c++;
res = res>>1;
}
return c;
}
};
// & 相同为1否则为0
// | 有一个为1 则就为1
// ^ 两值不同 为1 相同为0
// << 左边丢弃 右边补0
// >> 右边丢弃 左边补0 负数补1
边栏推荐
- LeetCode 1108 IP地址无效化[暴力] HERODING的LeetCode之路
- Node模块管理描述文件
- Win32com operation Excel
- Two understandings of Bayes formula
- 有哪些好用的工作汇报工具
- ByteDance proposes a lightweight and efficient new network mocovit, which has better performance than GhostNet and mobilenetv3 in CV tasks such as classification and detection!
- postman关联,完成接口自动化测试
- 潤邁德醫療通過上市聆訊:預計虧損將增加,霍雲飛兄弟持股約33%
- 案例驱动 :从入门到掌握Shell编程详细指南
- Server socket program
猜你喜欢
随机推荐
Stack awareness - stack overflow instance (ret2libc)
Encryption crash, is there a future for Web3 games? In depth discussion of 5 papers
Vit is crazy, 10+ visual transformer model details
Leetcode 1108 IP address invalidation [violence] the leetcode path of heroding
Hain's law and Feynman's learning method
tcpserver开启多线程处理
WXML模板语法、WXSS模板样式、全局配置、页面配置和网络数据请求
MySQL common interview questions
国元期货开户可靠吗?新手如何安全开户?
Redis配置与优化
企业高管收入杂谈
The way for programmers to learn
Show you how to distinguish several kinds of parallelism
List set to comma concatenated string
LeetCode 1108 IP地址无效化[暴力] HERODING的LeetCode之路
PHP输出函数
Stack cognition -- basic use of reverse IDA tools
Basic file operation
nest.js实战之模块依赖传递性
EtherCAT igh master station controls Esther servo to return to zero









