列表

详情


100393. 矩阵中的蛇

大小为 n x n 的矩阵 grid 中有一条蛇。蛇可以朝 四个可能的方向 移动。矩阵中的每个单元格都使用位置进行标识: grid[i][j] = (i * n) + j

蛇从单元格 0 开始,并遵循一系列命令移动。

给你一个整数 n 表示 grid 的大小,另给你一个字符串数组 commands,其中包括 "UP""RIGHT""DOWN""LEFT"。题目测评数据保证蛇在整个移动过程中将始终位于 grid 边界内。

返回执行 commands 后蛇所停留的最终单元格的位置。

 

示例 1:

输入:n = 2, commands = ["RIGHT","DOWN"]

输出:3

解释:

0 1
2 3
0 1
2 3
0 1
2 3

示例 2:

输入:n = 3, commands = ["DOWN","RIGHT","UP"]

输出:1

解释:

0 1 2
3 4 5
6 7 8
0 1 2
3 4 5
6 7 8
0 1 2
3 4 5
6 7 8
0 1 2
3 4 5
6 7 8

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: int finalPositionOfSnake(int n, vector<string>& commands) { } };

javascript 解法, 执行用时: 1 ms, 内存消耗: 53.6 MB, 提交时间: 2024-11-21 07:38:37

/**
 * @param {number} n
 * @param {string[]} commands
 * @return {number}
 */
var finalPositionOfSnake = function(n, commands) {
    let i = 0, j = 0;
    for (const s of commands) {
        switch (s[0]) {
            case 'U': i--; break;
            case 'D': i++; break;
            case 'L': j--; break;
            default:  j++;
        }
    }
    return i * n + j;
};

rust 解法, 执行用时: 3 ms, 内存消耗: 2.2 MB, 提交时间: 2024-11-21 07:38:01

impl Solution {
    pub fn final_position_of_snake(n: i32, commands: Vec<String>) -> i32 {
        let mut i = 0;
        let mut j = 0;
        for s in commands {
            match s.as_bytes()[0] {
                b'U' => i -= 1,
                b'D' => i += 1,
                b'L' => j -= 1,
                _    => j += 1,
            }
        }
        i * n + j
    }
}

cpp 解法, 执行用时: 22 ms, 内存消耗: 34.3 MB, 提交时间: 2024-08-12 09:46:14

class Solution {
public:
    int finalPositionOfSnake(int n, vector<string>& commands) {
        int i = 0, j = 0;
        for (auto& s : commands) {
            switch (s[0]) {
                case 'U': i--; break;
                case 'D': i++; break;
                case 'L': j--; break;
                default:  j++;
            }
        }
        return i * n + j;
    }
};

java 解法, 执行用时: 1 ms, 内存消耗: 43.4 MB, 提交时间: 2024-08-12 09:45:32

class Solution {
    public int finalPositionOfSnake(int n, List<String> commands) {
        int i = 0;
        int j = 0;
        for (String s : commands) {
            switch (s.charAt(0)) {
                case 'U' -> i--;
                case 'D' -> i++;
                case 'L' -> j--;
                default  -> j++;
            }
        }
        return i * n + j;
    }
}

golang 解法, 执行用时: 3 ms, 内存消耗: 4.4 MB, 提交时间: 2024-08-12 09:37:43

func finalPositionOfSnake(n int, commands []string) int {
    pos := 0
    t := map[string]int{
        "DOWN": n, 
        "UP": -n, 
        "RIGHT": 1, 
        "LEFT": -1,
    }
    for _, c := range commands {
        pos += t[c]
    }
    return pos
}

python3 解法, 执行用时: 47 ms, 内存消耗: 16.3 MB, 提交时间: 2024-08-12 09:36:15

class Solution:
    def finalPositionOfSnake(self, n: int, commands: List[str]) -> int:
        pos = 0
        t = {'DOWN': n, 'UP': -n, 'RIGHT': 1, 'LEFT': -1}
        for c in commands:
            pos += t[c]
        return pos

python3 解法, 执行用时: 41 ms, 内存消耗: 16.5 MB, 提交时间: 2024-08-12 09:35:07

class Solution:
    def finalPositionOfSnake(self, n: int, commands: List[str]) -> int:
        pos = 0
        for c in commands:
            if c == 'DOWN':
                pos += n
            elif c == 'UP':
                pos -= n
            elif c == 'RIGHT':
                pos += 1
            elif c == 'LEFT':
                pos -= 1
            else:
                pass
        return pos

上一题