Skip to content

Commit 4f464a1

Browse files
committed
Time: 0 ms (100.00%), Space: 41.9 MB (86.25%) - LeetHub
1 parent 1ad954b commit 4f464a1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int[] decrypt(int[] code, int k) {
3+
int[] res = new int[code.length];
4+
if (k == 0) return res;
5+
//Define the initial window and initial sum
6+
int start = 1, end = k, sum = 0;
7+
if (k < 0) {//If k < 0, the starting point will be end of the array.
8+
k = -k;
9+
start = code.length - k;
10+
end = code.length - 1;
11+
}
12+
for (int i = start; i <= end; i++) sum += code[i];
13+
//Scan through the code array as i moving to the right, update the window sum.
14+
for (int i = 0; i < code.length; i++) {
15+
res[i] = sum;
16+
sum -= code[(start++) % code.length];
17+
sum += code[(++end) % code.length];
18+
}
19+
return res;
20+
}
21+
}

0 commit comments

Comments
 (0)