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
            
                
        
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐