Defuse the Bomb
给一个循环数组arr和一个k, 给了k的规则, 求最后的数组.
class Solution {
public int[] decrypt(int[] code, int k) {
int n = code.length;
int[] nums = new int[3*n];
for(int i = 0; i < n; i++) {
nums[i] = code[i];
nums[i+n] = code[i];
nums[i+n+n] = code[i];
}
int[] res = new int[n];
if(k == 0)
return res;
else if(k > 0) {
for(int i = 0; i < n; i++) {
int sum = 0;
for(int j = 0; j < k; j++) {
sum += nums[n + i + j + 1];
}
res[i] = sum;
}
} else {
for(int i = 0; i < n; i++) {
int sum = 0;
for(int j = 0; j < Math.abs(k); j++) {
sum += nums[n + i - j - 1];
}
res[i] = sum;
}
}
return res;
}
}