这里可以将obstacles存在set中,这里有一个坐标hash的问题,Python用tuple可以直接搞,C++需要自己实现hash算法。可以用set解决。
class Solution:
def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
x = y = di = 0
obstacleSet = set(map(tuple, obstacles))
ans = 0
for cmd in commands:
if cmd == -2: #left
di = (di - 1) % 4
elif cmd == -1: #right
di = (di + 1) % 4
else:
for k in range(cmd):
if (x + dx[di], y + dy[di]) not in obstacleSet:
x += dx[di]
y += dy[di]
ans = max(ans, x*x + y*y)
else:
break
return ans