[LeetCode] Challenge log 657

657. Judge Route Circle

Simple, no need to comment


Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.

The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L(Left), U (Up) and D (down). The output should be true or false representing whether the robot makes a circle.

Example 1:

1
2
Input: "UD"
Output: true

Example 2:

1
2
Input: "LL"
Output: false

Soulution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
def judgeCircle(self, moves):
"""
:type moves: str
:rtype: bool
"""
U = 0
R = 0
for i in moves:
if i == 'U':
U += 1
elif i == 'D':
U -= 1
elif i == 'R':
R += 1
else:
R -= 1
return (U == 0 and R == 0)
0%