链接:
https://leetcode.com/problems/judge-route-circle/description/
题目:
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:
Input: “UD”
Output: true
Example 2:
Input: “LL”
Output: false
思路:
dict
或者elif
Answer:
dict
class Solution:
def judgeCircle(self, moves):
"""
:type moves: str
:rtype: bool
"""
table = {
'R': 1,
'L': -1,
'U': 1.5,
'D': -1.5
}
sum = 0
for move in moves:
sum += table[move]
return sum==0
elif
class Solution(object):
def judgeCircle(self, moves):
x = y = 0
for move in moves:
if move == 'U': y -= 1
elif move == 'D': y += 1
elif move == 'L': x -= 1
elif move == 'R': x += 1
return x == y == 0