Window Sum(sliding window)
Description
Example
class Solution:
"""
@param nums: a list of integers.
@param k: length of window.
@return: the sum of the element inside the window at each moving.
"""
def winSum(self, nums, k):
# write your code here
sum = 0
res = []
cnt = 0
nlen = len(nums)
for i in range(nlen):
sum += nums[i]
cnt += 1
if cnt > k:
sum -= nums[i - k]
cnt -= 1
if cnt == k:
res.append(sum)
return resLast updated