My answer: similar logic as the question "Number of 1 Bits"
class Solution { public int hammingDistance(int x, int y) { if (x == y) { return 0; } int dist = 0; while (x!=0 || y!=0) { int xBit = x & 1; int yBit = y & 1; if (xBit != yBit) { dist ++; } x = x >>> 1; y = y >>> 1; } return dist; } }
No comments:
Post a Comment