Last active
September 21, 2017 09:26
-
-
Save freelze/555fc9c58cde57fbfc291d9d4ba0d5cc to your computer and use it in GitHub Desktop.
Runtime: 6 ms , Your runtime beats 2.52 % of cpp submissions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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