Defuse the Bomb LeetCode solution in Java

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

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0