1. 程式人生 > >657. 機器人能否返回原點

657. 機器人能否返回原點

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