-
Notifications
You must be signed in to change notification settings - Fork 0
/
defuse_the_bomb.java
38 lines (38 loc) · 956 Bytes
/
defuse_the_bomb.java
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
class Solution {
public int[] decrypt(int[] code, int k) {
int n = code.length;
int[] ans = new int[n];
if(k == 0){
return ans;
}
else if(k > 0){
int c = 0;
int j;
int temp;
for(int i = 0; i < n; i++){
j = (i == n - 1 ? 0 :i + 1);
temp = 0;
c = 0;
while(c < k){
temp += code[j];
j = (j + 1) % n;
c++;
}
ans[i] = temp;
}
}
else{
int j, temp;
for(int i = 0; i < n; i++){
j = (i + n - (-1 * k)) % n;
temp = 0;
while(j != i){
temp += code[j];
j = (j + 1) % n;
}
ans[i] = temp;
}
}
return ans;
}
}