657. 机器人能否返回原点

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28018283/article/details/83341546

python

  1. 机器人能否返回原点
124 ms
class Solution(object):
    def judgeCircle(self, moves):
        """
        :type moves: str
        :rtype: bool
        """
        x = 0
        y = 0
        for i in moves:
            if i == "U":
                y +=1
            elif i == "D":
                y +=-1
            elif i == "L":
                x +=-1
            elif i == "R":
                x +=1
        if x ==0 and y == 0:
            return True
        else:
            return False

python

32 ms
class Solution(object):
    def judgeCircle(self, moves):
        """
        :type moves: str
        :rtype: bool
        """
        l = moves.count('L')
        r = moves.count('R')
        u = moves.count('U')
        d = moves.count('D')
        if l==r and u==d:
            return True
        else:
            return False
            
                
        

猜你喜欢

转载自blog.csdn.net/qq_28018283/article/details/83341546