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
            
                
        
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章