judge route circle

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 tothe original place.

The move sequence is represented by a string. And each move is represent by a character. The valid robot moves areR(Right),L(Left),U(Up) andD(down). The output should be true or false representing whether the robot makes a circle.

Example 1:

Input:"UD"Output:true

Example 2:

Input:"LL"Output:false

思路比较简单:就是看序列中R的数量是不是等于L的数量,同时U的数量是不是等于D的数量

class Solution(object):
    def judgeCircle(self, moves):
        """
        :type moves: str
        :rtype: bool
        """
        dic = {'R':0, 'L':0, 'U':0, 'D':0}
        for item in moves:
            dic[item] += 1
            
        if dic['R'] == dic['L'] and dic['U'] == dic['D']:
            return True
        else:
            return False

你可能感兴趣的:(judge route circle)