【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-28 11:04
已编辑
美团_后端实习生(实习员工)
一个2人:我说几个点吧,你的实习经历写的让人觉得毫无含金量,你没有挖掘你需求里的 亮点, 让人觉得你不仅打杂还摆烂。然后你的简历太长了🤣你这个实习经历看完,估计没几个人愿意接着看下去, sdk, 索引这种东西单拎出来说太顶真了兄弟,好好优化下简历吧
点赞 评论 收藏
分享
SinyWu:七院电话面的时候问我有没有女朋友,一听异地说你赶紧分。我:???
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务