657. Judge Route Circle
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.
Example 1:
Input: "UD"
Output: true
Example 2:
Input: "LL"
Output: false
思路:
主要就是衡量一个U和D的个数关系,以及R和L的个数关系,这里我还考虑了输入的 moves 是不同长度的空字符串的可能。
我的代码:
class Solution:
def judgeCircle(self, moves):
"""
:type moves: str
:rtype: bool
"""
if moves.count("U")==moves.count("L")==moves.count("R")==moves.count("D")==0:
return True
if len(moves)%2 != 0:
return False
if moves.count("U") == moves.count("D") and moves.count("R") == moves.count("L"):
return True
else:
return False