We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1ad954b commit 4f464a1Copy full SHA for 4f464a1
1652-defuse-the-bomb/1652-defuse-the-bomb.java
@@ -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