机器人能否返回原点-模拟657-python

机器人能否返回原点-模拟657-python,第1张

没看答案,只要向右移动和向左移动,向前移动和向后移动的次数一样,即可返回原点。

from collections import Counter

class Solution:
    def judgeCircle(self, moves: str) -> bool:
        move = Counter(moves)
        return move['U'] == move['D'] and move['L'] == move['R']
class Solution:
    def judgeCircle(self, moves: str) -> bool:
        up = down = left = right = 0
        
        for move in moves:
            if move == 'U':
                up += 1
            elif move == 'D':
                down += 1
            elif move == 'L':
                left += 1
            else:
                right += 1

        return up == down and left == right

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/943261.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-18
下一篇 2022-05-18

发表评论

登录后才能评论

评论列表(0条)

保存