657. 机器人能否返回原点

在二维平面上,有一个机器人从原点 (0, 0) 开始。给出它的移动顺序,判断这个机器人在完成移动后是否在 (0, 0) 处结束。

移动顺序由字符串表示。字符 move[i] 表示其第 i 次移动。机器人的有效动作有 R(右),L(左),U(上)和 D(下)。如果机器人在完成所有动作后返回原点,则返回 true。否则,返回 false。

注意:机器人“面朝”的方向无关紧要。 “R” 将始终使机器人向右移动一次,“L” 将始终向左移动等。此外,假设每次移动机器人的移动幅度相同

参考代码:

class Solution:
    def judgeCircle(self, moves: str):
        if set(moves) == "L" or set(moves) == "R" or set(moves) == "U" or set(moves) == "D":
            return False
        elif set(moves) == {'L', 'R'} and list(moves).count('L') == list(moves).count('R'):
            return True
        elif set(moves) == {'U', 'D'} and list(moves).count('U') == list(moves).count('D'):
            return True
        elif set(moves) == {'L', 'R', 'U', 'D'} and list(moves).count('L') == list(moves).count('R') and list(moves).count('U') == list(moves).count('D'):
            return True
        else:
            return False
posted @ 2020-03-12 22:33  HackerEarl  阅读(124)  评论(0)    收藏  举报