Leetcode Problem 657. Robot Return to Origin
657. Robot Return to Origin
AI Mock Interview
Leetcode Solutions
Counting Moves Approach
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
Initialize two counters,
horizontal
and
vertical
, to 0.
Iterate through each character in the
moves
string.
For each character, update the counters based on the move:
If the move is 'U', decrement
vertical
.
If the move is 'D', increment
vertical
.
If the move is 'L', decrement
horizontal
.
If the move is 'R', increment
horizontal
.
After processing all moves, check if both
horizontal
and
vertical
counters are 0.
If both counters are 0, return
true
indicating the robot is back at the origin.
If either counter is not 0, return
false
indicating the robot is not at the origin.
Using a Map to Track Moves
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...