3248.矩阵中的蛇
链接:3248.矩阵中的蛇
难度:Easy
标签:数组、字符串、模拟
简介:返回执行 commands 后蛇所停留的最终单元格的位置。
题解 1 - python
- 编辑时间:2024-11-21
- 执行用时:7ms
- 内存消耗:16.38MB
- 编程语言:python
- 解法介绍:模拟
dirs2 = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
class Solution:
    def finalPositionOfSnake(self, n: int, commands: List[str]) -> int:
        cur = [0, 0]
        for command in commands:
            if command == 'UP':
                cur[0] -= 1
            elif command == 'DOWN':
                cur[0] += 1
            elif command == 'LEFT':
                cur[1] -= 1
            elif command == 'RIGHT':
                cur[1] += 1
        return cur[0] * n + cur[1]