题目地址:https://leetcode.com/problems/judge-route-circle/description/
大意:The valid robot moves are R (Right), L (Left), U (Up) and D (down). 给一个字符串,让这个机器人按照字符串的字母走,看看能不能回到原点
其实很简单,看看R的个数和L的个数还有U的个数和D的个数这两个都要相同就能回来了。
class Solution:
def judgeCircle(self, moves):
"""
:type moves: str
:rtype: bool
"""
sp = list(moves)
if (sp.count('R') == sp.count('L') and sp.count('U') == sp.count('D')):
return True
else:
return False
a = Solution()
print (a.judgeCircle('LL'))
改进:
def judgeCircle2(self, moves):
"""
:type moves: str
:rtype: bool
"""
return moves.count('L') == moves.count('R') and moves.count('U') and moves.count('D')
知识点:
string字符串是可以直接用count()
方法的。返回值也不用判断True
和False
是可以直接返回判断表达式就可以了。