Question

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.

The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L (Left), U (Up) and D (down). The output should be true or false representing whether the robot makes a circle.

Examples:

Input: "UD"
Output: true

Input: "LL"
Output: false

Solution

class Solution(object):
    def judgeCircle(self, moves):
        """ :type moves: str :rtype: bool """
        x=0
        y=0
        for step in moves:
            if(step == 'L'):
                x-=1
            elif(step == 'R'):
                x+=1
            elif(step == 'U'):
                y+=1
            elif(step == 'D'):
                y-=1
        if(x==0 and y == 0):
            return True
        else:
            return False
res = Solution()
res.judgeCircle("LLRRUUDD")
if(res):
    print("true")
else:
    print("false")

很简单,不解释