leet-code/src/main/java/leetcode/editor/cn/HammingDistance.java

41 lines
946 B
Java
Raw Normal View History

2021-05-27 08:39:51 +08:00
//两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。
//
// 给出两个整数 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)
}