forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefuse the Bomb.cpp
33 lines (32 loc) · 929 Bytes
/
Defuse the Bomb.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public:
vector<int> decrypt(vector<int>& code, int k) {
vector<int>Ans(code.size(),0);
for(int i = 0;i<code.size();i++){
if (k > 0){
int total = 0;
for(int count = 1;count<=k;count++){
int num = i + count;
num = num % code.size();
total += code[num];
}
Ans[i] = total;
}
else if (k < 0){
int total = 0;
for(int count = -1;count>=k;count--){
int num = i + count;
if (num < 0){
num = code.size() + num;
}
total += code[num];
}
Ans[i] = total;
}
else{
Ans[i] = 0;
}
}
return Ans;
}
};