本文主要是介绍LeetCode461 Hamming Distance java The Hamming distance between two integers is the number osoluotion,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目要求:
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.
Note:
0 ≤ x
, y
< 231.
Example:
Input: x = 1, y = 4Output: 2Explanation: 1 (0 0 0 1) 4 (0 1 0 0)↑ ↑The above arrows point to positions where the corresponding bits are different.
注:该题目没有bug free
原因在于:创建循环条件的时候没有注意取最大值
public class Solution {public int hammingDistance(int x, int y) {int step = 0;while(Math.max(x, y) != 0) {int temple1 = x % 2;int temple2 = y % 2;if(temple1 != temple2) step++;x /= 2;y /= 2;}return step;}
}
这篇关于LeetCode461 Hamming Distance java The Hamming distance between two integers is the number osoluotion的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!