657. 机器人能否返回原点
https://leetcode-cn.com/problems/robot-return-to-origin/
解法一:
用两个计数器(水平、垂直)分别计算上下和左右方向的移动步数,初始都为0,若能回归初始状态,则说明返回原点
class Solution:
def judgeCircle(self, moves: str) -> bool:
vertical = 0 #垂直方向
horizontal = 0 #水平
for move in moves:
if move == 'R':
horizontal += 1
if move == 'L':
horizontal -= 1
if move == 'U':
vertical += 1
if move == 'D':
vertical -= 1
return vertical == 0 and horizontal == 0
最后更新于