LeetCode Question 657. Can the robot return to the origin?
I don't know where I am going, but I am already on my way!
Time is hurried, although I have never met, but I met Yusi, it is really a great fate, thank you for your visit!
Problem : On a two-dimensional plane, there is a robot starting from the origin (0, 0). Given its moving sequence, judge whether the robot ends at (0, 0) after completing its movement. The movement order is represented by a character string. The character move[i] represents its i-th move. The effective actions of the robot are R (right), L (left), U (up) and D (down). If the robot returns to the origin after completing all actions, it returns true. Otherwise, it returns false.
Note : The direction the robot "faces" does not matter. "R" will always move the robot to the right once, "L" will always move to the left, etc. In addition, it is assumed that the moving range of the robot is the same each time.
Code:
classSolution:defjudgeCircle(self, moves:str)->bool:
U = moves.count('U')
D = moves.count('D')
R = moves.count('R')
L = moves.count('L')if U == D and R == L:returnTruereturnFalse# 执行用时:36 ms, 在所有 Python3 提交中击败了98.99%的用户# 内存消耗:13.5 MB, 在所有 Python3 提交中击败了94.54%的用户
Algorithm description: Abstract the topic into a chessboard pattern, directly count the number of moves in the opposite direction, judge whether they are equal, and return the result.