呓语Beta 2.0 首页 镜头后 灶台前 捐赠者名单 有趣的小站

leetcode 657 Judge Route Circle

文档信息

leetcode 657 Judge Route Circle

解法一: 扫描字符串统计四种字符的个数,然后比较个数是否相同

class Solution(object):
    def judgeCircle(self, moves):
        """
        :type moves: str
        :rtype: bool
        """
        countL = countR = countD = countU = 0
                                                   
        for step in moves:
            if step == 'L':
                countL += 1
            elif step == 'R':
                countR += 1
            elif step == 'U':
                countU += 1
            elif step == 'D':
                countD += 1
                                                                                                                                                           
        return countL == countR and countU == countD

解法二: 利用collections.Counter统计四种字符个数

def judgeCircle(self, moves):
    c = collections.Counter(moves)
    return c['L'] == c['R'] and c['U'] == c['D']

附录: collections

捐赠支持

可以使用支付宝转帐给richard.ma.19850509#gmail.com(请将#替换为@),谢谢!

Fork me on GitHub