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
本文介绍了一种通过分析指令序列来判断机器人是否返回到初始位置的方法。主要关注上下(U/D)与左右(L/R)指令的数量关系,以此确定机器人是否完成了一个圆形路径并返回起点。
1349

被折叠的 条评论
为什么被折叠?



