leetcode刷题250天(20)——1041. 困于环中的机器人

class Solution(object):
    def isRobotBounded(self, instructions):

        # 规定方向 默认向北 逆时针旋转 建立坐标轴
        front, x, y = 0 ,0 ,0
        #    0
        # 1     3
        #    2 

        # 任何情况最多重复4次 如果回到原点 说明转圈
        for char in instructions*4:
            if char == "G":
                if   front == 0: y += 1
                elif front == 1: x -= 1
                elif front == 2: y -= 1
                else: x += 1

            elif char == "L":
                front = (front+1) % 4
                
            # 倒转求补防溢出
            elif char == "R":
                front = (front-1 + 4) % 4
    
        return True if x == 0 and y == 0 else False

你可能感兴趣的:(leetcode,leetcode,算法)