Robot Bounded In Circle 掃地機器人
class Solution:
def isRobotBounded(self, instructions: str) -> bool:
coordinate = (0, 0)
direction = (0, 1)
for i in range(4):
for instruction in instructions:
if instruction == 'R':
if direction == (0, 1):
direction = (1, 0)
elif direction == (1, 0):
direction = (0, -1)
elif direction == (0, -1):
direction = (-1, 0)
elif direction == (-1, 0):
direction = (0, 1)
if instruction == 'L':
if direction == (0, 1):
direction = (-1, 0)
elif direction == (-1, 0):
direction = (0, -1)
elif direction == (0, -1):
direction = (1, 0)
elif direction == (1, 0):
direction = (0, 1)
if instruction == 'G':
coordinate = (coordinate[0] + direction[0], coordinate[1] + direction[1])
return coordinate == (0, 0)重構程式碼(增加閱讀性)
重構程式碼(理解此道題目)
Last updated