美文网首页
judge route circle

judge route circle

作者: z_star | 来源:发表于2017-12-13 23:31 被阅读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 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

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