2024/10/07 灵茶题单 滑动窗口

This commit is contained in:
Cool 2024-10-07 21:00:41 +08:00
parent ed7d71e58d
commit 17c02133fe
1 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,57 @@
package com.cool.ling_cha_mount.sliding_windows;
import org.junit.Test;
import java.util.Arrays;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/10/07/20:48
* @Description:
*/
public class Num1652 {
public int[] decrypt(int[] code, int k) {
int[] res = new int[code.length];
if (k > 0) {
int sum = 0;
int right = 1;
for (int i = 0; i < k; i++) {
sum += code[right++];
}
for (int i = 0; i < code.length; i++) {
if (right >= code.length) {
right = 0;
}
res[i] = sum;
sum += code[right++];
if (i != code.length - 1) {
sum -= code[i + 1];
}
}
} else if (k < 0) {
int right = code.length - 2;
int sum = 0;
k = -k;
for (int i = code.length - 1; i > code.length - 1 - k; i--) {
sum += code[right--];
}
for (int i = code.length - 1; i >= 0; i--) {
if (right < 0) {
right = code.length - 1;
}
res[i] = sum;
sum += code[right--];
if (i != 0) {
sum -= code[i - 1];
}
}
}
return res;
}
@Test
public void test(){
decrypt(new int[]{5,7,1,4},3);
}
}