LintCode 604. Window Sum

題目:
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 the sum of the element inside the window at each moving.

思路:

  1. 先算第一個框框總和
  2. 接下來只要把框框+後一個,-前一個,就可以得到一個新的總和

代碼:

public:
    /**
     * @param nums: a list of integers.
     * @param k: length of window.
     * @return: the sum of the element inside the window at each moving.
     */
    vector winSum(vector &nums, int k) {
        // write your code here
        if(nums.size()==0 || nums.size() < k || k == 0)
            return {};
        
        int currSum =0;
        vector ans;
        
        for(int i =0; i

你可能感兴趣的:(LintCode 604. Window Sum)