-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution1652.java
More file actions
28 lines (28 loc) · 880 Bytes
/
Copy pathSolution1652.java
File metadata and controls
28 lines (28 loc) · 880 Bytes
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
class Solution1652 {
public int[] decrypt(int[] code, int k) {
int len = code.length;
int[] res = new int[len];
if (k == 0) {
for (int i = 0; i < len; i++) {
res[i] = 0;
}
} else if (k > 0) {
for (int i = 0; i < k; i++) {
res[0] += code[(i+1) % len];
}
int st = 1, end = k;
for (int i = 1; i < len; i++) {
res[i] = res[i - 1] + code[++end % len] - code[st++];
}
} else {
for (int i = 0; i > k; i--) {
res[0] += code[(len + i - 1) % len];
}
int st = len - 1, end = len + k;
for (int i = 1; i < len; i++) {
res[i] = res[i - 1] + code[(++st + len) % len] - code[end++ % len];
}
}
return res;
}
}