LeetCode-657. Judge Route Circle

来源:互联网 发布:vb程序改错数字金字塔 编辑:程序博客网 时间:2024/06/05 23:52

题目链接:657. Judge Route Circle


题目大意:有一个机器人,可以上下左右走,现在给你一个字符串,包含U,D,L,R四种字符,请判断机器人走完之后,是否回到原来的位置上了。


常规解法:

class Solution(object):    def judgeCircle(self, moves):        x=0        y=0        mList = list(moves)        for m in mList:            if (m == 'U'):                y += 1            elif (m == 'D'):                y -= 1            elif (m == 'R'):                x += 1            elif (m == 'L'):                x -= 1        if (x == 0 and y == 0):            return True        else:            return False

利用内建函数的解法(一行):

class Solution(object):    def judgeCircle(self, moves):        return moves.count('L') == moves.count('R') and moves.count('U') == moves.count('D')

因为这个题目实在是没有可说的,就这样吧。

以上。

原创粉丝点击