Leetcode # 1041 (Python): Robot Bounded In Circle
Robot Bounded In Circle
Problem
Solution
class Solution:
def isRobotBounded(self, instructions: str) -> bool:
direction = ["N","E","S","W"]
action =[[0,1],[1,0],[0,-1],[-1,0]]
# Function to determine next cordinate and direction
def get_cord_dir(cur_cord,cur_dir,instruct):
i = direction.index(cur_dir)
if instruct == "R":
i = (i+1)%len(direction)
elif instruct == "L":
i -= 1
elif instruct == "G":
cur_cord[0] += action[i][0]
cur_cord[1] += action[i][1]
cur_dir = direction[i]
return cur_cord,cur_dir
instructions = list(instructions)
cur_cord = [0,0]
cur_dir = "N"
# Run 1 cycle of instructions
for inst in instructions:
cur_cord,cur_dir = get_cord_dir(cur_cord,cur_dir,inst)
# If the cordinate is back at [0,0] or direction is not at north after 1 cycle,
# Repeating the instruction would result in a circle
return cur_cord == [0,0] or cur_dir != "N"
Reference
この問題について(Leetcode # 1041 (Python): Robot Bounded In Circle), 我々は、より多くの情報をここで見つけました https://velog.io/@ahn16/Leetcode-1041-Python-Robot-Bounded-In-Circleテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol