Jsc 提问:2120. 执行所有后缀指令 - 力扣(LeetCode)
英雄哥你好,这道有关方向的模拟题我不太明白,不太明白的点在下面的代码中有标注
class Solution {
public:
vector<int> executeInstructions(int n, vector<int>& startPos, string s) {
int m = s.size();
vector<int> ans;
for(int i = 0; i < m; ++i){
int x = startPos[0], y = startPos[1];
int cnt = m - i; //这里是为什么?
for(int j = i; j < m; ++j){
char ch = s[j];
if(ch == 'L'){
y--;
}else if(ch == 'R'){
y++;
}else if(ch == 'U'){
x--;
}else{
x++;
}
if(x < 0 || x >= n || y < 0 || y >= n){
cnt = j - i;//这里是为什么?
break;
}
}
ans.push_back(cnt);
}
return ans;
}
};
1、int cnt = m - i; 代表一直没有出界的情况...