1652.拆炸弹
思路
暴力模拟即可,记得用 % 取余。
代码
class Solution {
public:
vector<int> decrypt(vector<int>& code, int k) {
vector<int> res;
if (k == 0) {
for (int i =0 ; i < code.size(); i++) res.push_back(0);
return res;
}
if (k > 0) {
for (int i = 0 ; i < code.size(); i++) {
int sum = 0;
for (int j = 1; j <= k; j++) {
sum += code[(i + j) % code.size()];
}
res.push_back(sum);
}
} else if (k < 0) {
k = abs(k);
for (int i = 0; i < code.size(); i++) {
int sum = 0;
for (int j = 1; j <= k; j++) {
sum += code[(i - j + code.size()) % code.size()];
}
res.push_back(sum);
}
}
return res;
}
};