【LeetCode】【Python】657. Judge Route Circle

Question

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 to the original place.

The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L (Left), U (Up) and D (down). The output should be true or false representing whether the robot makes a circle.

Examples:

Input: "UD"
Output: true

Input: "LL"
Output: false

Solution

class Solution(object):
    def judgeCircle(self, moves):
        """ :type moves: str :rtype: bool """
        x=0
        y=0
        for step in moves:
            if(step == 'L'):
                x-=1
            elif(step == 'R'):
                x+=1
            elif(step == 'U'):
                y+=1
            elif(step == 'D'):
                y-=1
        if(x==0 and y == 0):
            return True
        else:
            return False
res = Solution()
res.judgeCircle("LLRRUUDD")
if(res):
    print("true")
else:
    print("false")

很简单,不解释

全部评论

相关推荐

10-11 17:45
门头沟学院 Java
走吗:别怕 我以前也是这么认为 虽然一面就挂 但是颇有收获!
点赞 评论 收藏
分享
把球:这个听过,你加了就会发现是字节的hr
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务