0
点赞
收藏
分享

微信扫一扫

[220109] Robot Bounded In Circle

何晓杰Dev 2022-02-05 阅读 48
class Solution:
    def isRobotBounded(self, instructions):

        # 初始设置:[up, left, down, right]
        direction = [[0, 1], [-1, 0], [0, -1], [1, 0]]
        rotation = 0
        x, y = 0, 0

        # N - 0, W - 1, S - 2, E - 3
        for i in instructions:
            if i == 'L':
                rotation = (rotation + 1) % 4
            elif i == 'R':
                rotation = (rotation + 3) % 4
            else:
                x += direction[rotation][0]
                y += direction[rotation][1]

        # 如果最终朝向不变,一定要在原点
        return x == 0 and y == 0 or rotation != 0

 

举报

相关推荐

0 条评论