题目链接
分析
水题水做,直接计算即可,模拟也行
代码实现
class Solution {
public:
int finalPositionOfSnake(int n, vector<string>& commands) {
int j = count(commands.begin(), commands.end(), "RIGHT") - count(commands.begin(), commands.end(), "LEFT");
int i = count(commands.begin(), commands.end(), "DOWN") - count(commands.begin(), commands.end(), "UP");
return (i * n) + j;
}
};
复杂度分析
- 时间复杂度:$O(m)$,$m$ 为 $commands$ 的长度
- 空间复杂度:$O(1)$