Skip to content

Instantly share code, notes, and snippets.

@freelze
Last active September 21, 2017 09:26
Show Gist options
  • Save freelze/555fc9c58cde57fbfc291d9d4ba0d5cc to your computer and use it in GitHub Desktop.
Save freelze/555fc9c58cde57fbfc291d9d4ba0d5cc to your computer and use it in GitHub Desktop.
Runtime: 6 ms , Your runtime beats 2.52 % of cpp submissions.
class Solution {
public:
int hammingDistance(int x, int y) {
const int MAX = 10000;
bool two_x[MAX] = {0}, two_y[MAX] = {0};
int i = MAX-1, j = MAX-1;
int point = 0;
while(x>=2)
{
two_x[i--] = x%2;
x= x/2;
}
if(x == 1)
two_x[i] = 1;
while(y>=2)
{
two_y[j--] = y%2;
y= y/2;
}
if(y == 1)
two_y[j] = 1;
int k = i>j ? j : i ;
while(k<MAX)
{
if(two_x[k] !=two_y[k++])
point++;
}
return point;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment