Class Solution

java.lang.Object
g0601_0700.s0657_robot_return_to_origin.Solution

public class Solution extends Object
657 - Robot Return to Origin.<p>Easy</p> <p>There is a robot starting at the position <code>(0, 0)</code>, the origin, on a 2D plane. Given a sequence of its moves, judge if this robot <strong>ends up at</strong> <code>(0, 0)</code> after it completes its moves.</p> <p>You are given a string <code>moves</code> that represents the move sequence of the robot where <code>moves[i]</code> represents its <code>i<sup>th</sup></code> move. Valid moves are <code>'R'</code> (right), <code>'L'</code> (left), <code>'U'</code> (up), and <code>'D'</code> (down).</p> <p>Return <code>true</code> <em>if the robot returns to the origin after it finishes all of its moves, or</em> <code>false</code> <em>otherwise</em>.</p> <p><strong>Note</strong>: The way that the robot is &ldquo;facing&rdquo; is irrelevant. <code>'R'</code> will always make the robot move to the right once, <code>'L'</code> will always make it move left, etc. Also, assume that the magnitude of the robot&rsquo;s movement is the same for each move.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> moves = &ldquo;UD&rdquo;</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> moves = &ldquo;LL&rdquo;</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> The robot moves left twice. It ends up two &ldquo;moves&rdquo; to the left of the origin. We return false because it is not at the origin at the end of its moves.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= moves.length <= 2 * 10<sup>4</sup></code></li> <li><code>moves</code> only contains the characters <code>'U'</code>, <code>'D'</code>, <code>'L'</code> and <code>'R'</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • judgeCircle

      public boolean judgeCircle(String moves)