604.Window Sum
1.Decription(Easy)
Given an array of n integer, and a moving window(size k), move the window at each iteration from the start of the array, find thesum
of the element inside the window at each moving.
Example
For array[1,2,7,8,5]
, moving window size k =3
.
1 + 2 + 7 = 10
2 + 7 + 8 = 17
7 + 8 + 5 = 20
return[10,17,20]
2.Code
设置存放结果的sums数组,先计算出sums[0],再从sums[1]开始,sums[i]=sums[i-1]-nums[i-1]+nums[i+k-1];剪去第一个数加上最后一个
Last updated