LeetCode 461 Hamming Distance(汉明距离)

简介:

Q: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.
注:两个等长字符串之间的汉明距离(英语: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

S:
public class Solution {

      public int hammingDistance(int x, int y) {
          return Integer.bitCount(x ^ y);
      }

}

bitCount 自实现方案:

              int d = 0;
    int bitxor = x ^ y;
     while (bitxor > 0){
        if (bitxor % 2 == 1){
            d++;
        }
        bitxor = bitxor >> 1;
    }
    return d;
目录
相关文章
|
6月前
【Leetcode -461.汉明距离 -482.密钥格式化】
【Leetcode -461.汉明距离 -482.密钥格式化】
26 0
LeetCode 461. 汉明距离
LeetCode 461. 汉明距离
67 0
LeetCode 461. 汉明距离
|
数据库 C语言
LeetCode 72. Edit Distance
给定两个单词word1和word2,找到将word1转换为word2所需的最小操作数。 您对单词允许以下3个操作: 插入一个字符 删除一个字符 替换一个字符
42 0
LeetCode 72. Edit Distance
|
算法 Java
《LeetCode刷题》—461.汉明距离
《LeetCode刷题》—461.汉明距离
82 0
《LeetCode刷题》—461.汉明距离
Leetcode-Easy 72. Edit Distance
Leetcode-Easy 72. Edit Distance
66 0
Leetcode-Easy 72. Edit Distance
|
算法
​LeetCode刷题实战477:汉明距离总和
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
103 0
|
算法
​LeetCode刷题实战461:汉明距离
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
77 0
LeetCode 训练场:461. 汉明距离
LeetCode 训练场:461. 汉明距离
89 0
|
算法 Python
<LeetCode天梯>Day047 汉明距离(位运算+内置函数) | 初级算法 | Python
<LeetCode天梯>Day047 汉明距离(位运算+内置函数) | 初级算法 | Python
<LeetCode天梯>Day047 汉明距离(位运算+内置函数) | 初级算法 | Python
LeetCode之Hamming Distance
LeetCode之Hamming Distance
98 0