美文网首页
657. Judge Route Circle

657. Judge Route Circle

作者: 腹黑君 | 来源:发表于2017-08-26 17:25 被阅读0次

    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.

    Input: "UD"
    Output: true
    

    python的办法

        c = collections.Counter(moves)
        return c['L'] == c['R'] and c['U'] == c['D']
    

    傻了吧唧的办法

        def judgeCircle(self, moves):
            """
            :type moves: str
            :rtype: bool
            """
            fre = 0
            dir = {'R':0,'L':0,'U':0,'D':0}
            for i in moves:
                if i in dir.keys():
                    dir[i] += 1
            if dir['R'] == dir['L'] and dir['U'] == dir['D']:
                return True
            else:
                return False
    

    相关文章

      网友评论

          本文标题:657. Judge Route Circle

          本文链接:https://www.haomeiwen.com/subject/orcvdxtx.html