leet-code/src/main/java/leetcode/editor/cn/HammingDistance.java
2021-05-28 08:13:38 +08:00

43 lines
969 B
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。
//
// 给出两个整数 x 和 y计算它们之间的汉明距离。
//
// 注意:
//0 ≤ x, y < 231.
//
// 示例:
//
//
//输入: x = 1, y = 4
//
//输出: 2
//
//解释:
//1 (0 0 0 1)
//4 (0 1 0 0)
// ↑ ↑
//
//上面的箭头指出了对应二进制位不同的位置。
//
// Related Topics 位运算
// 👍 426 👎 0
package leetcode.editor.cn;
//461:汉明距离
public class HammingDistance {
public static void main(String[] args) {
//测试代码
Solution solution = new HammingDistance().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int hammingDistance(int x, int y) {
return Integer.bitCount(x ^ y);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}