[
string
simulation
]
Leetcode 0657 Robot Return to Origin
Problem statement
https://leetcode.com/problems/robot-return-to-origin/
Solution
Just do what is asked, simulate the process.
Complexity
Time complexity is O(n)
, space is O(1)
.
Code
class Solution:
def judgeCircle(self, moves):
x, y = 0, 0
for s in moves:
if s == "R": x += 1
elif s == "L": x -= 1
elif s == "U": y += 1
else: y -= 1
return x == 0 and y == 0