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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容