【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-22 12:03
山东大学 Java
程序员小白条:26届一般都得有实习,项目可以随便写的,如果不是开源社区的项目,随便包装,技术栈也是一样,所以本质应该找学历厂,多投投央国企和银行,技术要求稍微低一点的,或者国企控股那种,纯互联网一般都得要干活
应届生简历当中,HR最关...
点赞 评论 收藏
分享
10-13 12:53
已编辑
湖北工业大学 前端工程师
小海c:包装一下,第一个感觉是字节青训营的那个,后面那个是黑马的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务