-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDefuse The Bomb.cpp
41 lines (41 loc) · 962 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
34
35
36
37
38
39
40
41
class Solution {
public:
vector<int> decrypt(vector<int>& code, int k) {
int i, n = code.size();
vector<int> ans(n);
if(k == 0)
return ans;
else if(k == n)
return code;
else if(k > n)
k %= n;
else
k %= -n;
for(i = 0; i < n; i++)
{
int temp = k;
if(temp > 0)
{
int idx = i + 1;
while(temp--)
{
ans[i] += code[idx % n];
idx++;
}
}
else
{
int idx = i - 1;
while(temp++ < 0)
{
if(idx < 0)
ans[i] += code[n + idx];
else
ans[i] += code[idx];
idx--;
}
}
}
return ans;
}
};