-
Notifications
You must be signed in to change notification settings - Fork 0
/
1652. Defuse the Bomb.cpp
50 lines (43 loc) · 1.04 KB
/
1652. 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// https://leetcode.com/problems/defuse-the-bomb/
class Solution
{
public:
vector<int> decrypt(vector<int> &code, int k)
{
if (k == 0)
return vector<int>(code.size(), 0);
vector<int> result(code.size());
if (k > 0)
{
for (int i = 0; i < code.size(); ++i)
{
result[i] = 0;
int j = i + 1;
int t = k;
while (t--)
{
j %= code.size();
result[i] += code[j];
j++;
}
}
}
else
{
for (int i = code.size() - 1; i >= 0; --i)
{
result[i] = 0;
int j = i - 1;
int t = -k;
while (t--)
{
if (j < 0)
j += code.size();
result[i] += code[j];
j--;
}
}
}
return result;
}
};