-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowSum.java
More file actions
45 lines (38 loc) · 870 Bytes
/
WindowSum.java
File metadata and controls
45 lines (38 loc) · 870 Bytes
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
34
35
36
37
38
39
40
41
42
43
44
45
package additional_question;
import java.util.ArrayList;
import java.util.List;
public class WindowSum {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Integer> input = new ArrayList<Integer>();
input.add(1);
input.add(2);
input.add(3);
input.add(4);
input.add(5);
input.add(6);
input.add(7);
input.add(8);
input.add(9);
List<Integer> result = GetSum(input,3);
System.out.println();
}
public static List<Integer> GetSum(List<Integer> A, int k) {
List<Integer> result = new ArrayList<Integer>();
if(A.size()<=0||A==null||k<=0){
return result;
}
int count = 0;
for(int i=0; i<A.size(); i++){
if(count>=k-1){
int tmpSum = 0;
for(int j = count; j>count-k; j--){
tmpSum = tmpSum+A.get(j).intValue();
}
result.add(tmpSum);
}
count++;
}
return result;
}
}