Leetcode Problem 657. Robot Return to Origin

657. Robot Return to Origin

Leetcode Solutions

Counting Moves Approach

  1. Initialize two counters, horizontal and vertical, to 0.
  2. Iterate through each character in the moves string.
  3. 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.
  4. After processing all moves, check if both horizontal and vertical counters are 0.
  5. If both counters are 0, return true indicating the robot is back at the origin.
  6. If either counter is not 0, return false indicating the robot is not at the origin.
UML Thumbnail

Using a Map to Track Moves

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...