-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathArrayAverages.java
33 lines (29 loc) · 1010 Bytes
/
ArrayAverages.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package ds.sliding;
import java.util.Arrays;
/**
* 求一个数组所有连续K个元素构成的子集的平均数
*
* @author yangyi 2020年05月13日23:09:33
*/
public class ArrayAverages {
private static double[] findAverages(int K, int[] array) {
double[] averages = new double[array.length - K + 1];
int windowStart = 0, windowEnd;
double windowSum = 0;
for (windowEnd = 0; windowEnd < array.length; windowEnd++) {
windowSum += array[windowEnd];
//如果K长度的范围到头了,就可以计算了
if (windowEnd >= K - 1) {
averages[windowStart] = windowSum / K;
windowSum -= array[windowStart];
windowStart++;
}
}
return averages;
}
public static void main(String[] args) {
int[] array = {1, 3, 2, 6, -1, 4, 1, 8, 2};
double[] result = findAverages(5,array);
System.out.println(Arrays.toString(result));
}
}