没看答案,只要向右移动和向左移动,向前移动和向后移动的次数一样,即可返回原点。
from collections import Counter
class Solution:
def judgeCircle(self, moves: str) -> bool:
move = Counter(moves)
return move['U'] == move['D'] and move['L'] == move['R']
class Solution:
def judgeCircle(self, moves: str) -> bool:
up = down = left = right = 0
for move in moves:
if move == 'U':
up += 1
elif move == 'D':
down += 1
elif move == 'L':
left += 1
else:
right += 1
return up == down and left == right
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)