# n = 20 x 20
# length = 3
# every move -> length + 1
# moves -> 4 directions
# ends if snake hit itself
# snake head at x = 10 y = 10
# interface SnakeGame {# moveSnake(snakeDirection);
# isGameOver();
# }
- 维护一个 队列 / 双端队列 deque 存储蛇的每个坐标(头在前)
- 移动时根据 direction 计算蛇头新坐标
- 检查是否撞到自己(新头出现在队列中)
- 若撞到,则设置 gameOver = true
- 否则把新头 push,尾部 pop(如果需要增长可不 pop)
VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 Meta、Google、Amazon 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备 Stripe 或类似工程向公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。
正文完