[C] Simple solution O(n)


  • 0
    P

    Tracking location of the robot which represent by (x,y) on x,y-coordinate.
    If (x,y)=(0.0) and there are no more move, means robot back to origin location at the end.

    bool judgeCircle(char* moves) {
        int x=0,y=0;
    
        while(*moves !='\0')
        {
            switch(*moves)
            {
                case 'U':
                    x++;
                    break;
                case 'D':
                    x--;
                    break;
                case 'R':
                    y++;
                    break;
                case 'L':
                    y--;
                    break;
                default:
                    printf("unexpected key\n");
            }
            moves++;
        }
        return !(x|y);
    }
    

Log in to reply
 

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