657. Judge Route Circle

题目地址: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()方法的。返回值也不用判断TrueFalse是可以直接返回判断表达式就可以了。

所有题目解题方法和答案代码地址:https://github.com/fredfeng0326/LeetCode
    原文作者:fred_33c7
    原文地址: https://www.jianshu.com/p/de6e2f6fefec
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞