Freshman's C solution.Easiest way to understand.


  • 0
    H
        int hammingDistance(int x, int y) 
       {
        int i=0,j=0;
        int sum=0;
        while (x!=0 && y!=0)
        {
            if (x%2 == y%2)
            {
                x=x/2;
                y=y/2;
            }
            else
            {
                sum++;
                x=x/2;
                y=y/2;
            }
        }
        if(x==0&&y==0) return sum;
        while(x==0 && y>0)
        {
            if(y%2 != 0)
            {
                sum++;
                y=y/2;
            }
            else
            {
                y=y/2;
            }
        }
        while(y==0 && x>0)
        {
            if(x%2 != 0)
            {
                sum++;
                x=x/2;
            }
            else
            {
                x=x/2;
            }
        }
        return sum;
    }
    

  • 0
    H
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.