初始位置 (0, 0) 处有一个机器人。给出它的一系列动作,判断这个机器人的移动路线是否形成一个圆圈,换言之就是判断它是否会移回到原来的位置。
移动顺序由一个字符串表示。每一个动作都是由一个字符来表示的。机器人有效的动作有 R
(右),L
(左),U
(上)和 D
(下)。输出应为 true 或 false,表示机器人移动路线是否成圈。
自己代码(python)
class Solution(object):
def judgeCircle(self, moves):
u=0
d=0
l=0
r=0
for i in moves:
if i =='U':
u=u+1
if i =='D':
d=d+1
if i=='L':
l=l+1
if i=='R':
r=r+1
if (u==d)and(l==r):
return True
else:
return False
别人代码(简洁)
return moves.count('L') == moves.count('R') and moves.count('U') == moves.count('D')
复数版
direction={'U':1,'D':-1,'L':-1j,'R':1j}
return sum([direction[item] for item in moves])==0