LeetCode Weekly Contest 13-HammingDistance【易】
1254 ワード
HammingDistance
The Hamming distance between two integers is thenumber of positions at which the corresponding bits are different.
Given two integers x and y, calculatethe 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 positionswhere the corresponding bits are different.
The Hamming distance between two integers is thenumber of positions at which the corresponding bits are different.
Given two integers x and y, calculatethe 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 positionswhere the corresponding bits are different.
class Solution {
public:
void itoa(unsigned char* p, int x)
{
int i = 0;
while(x > 0)
{
p[i ++] = x % 2 + '0';
x = x / 2;
//std::cout< 0 && b[j] > 0)
{
if(a[i] != b[j])
iResult ++;
i ++; j ++;
}
//std::cout< 0) { if(a[i] == '1') iResult ++; i ++; }
while(j <= 32 && b[j] > 0) { if(b[j] == '1') iResult ++; j ++; }
return iResult;
}
};