657. Judge Route Circle

2017-08-26  本文已影响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
上一篇下一篇

猜你喜欢

热点阅读