1588. 所有奇数长度子数组的和前缀和

Posted 幽殇默

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1588. 所有奇数长度子数组的和前缀和相关的知识,希望对你有一定的参考价值。


https://leetcode-cn.com/problems/sum-of-all-odd-length-subarrays/

class Solution {
public:
    int sumOddLengthSubarrays(vector<int>& arr) 
    {
        vector<int>ve; ve.push_back(0);
        int sum=0;
        for(int i=0;i<arr.size();i++)
        {
            sum+=arr[i];
            ve.push_back(sum);
        }
        int ans=0;
        for(int len=1;len<ve.size();len+=2)
        {
            for(int i=1;i+len-1<ve.size();i++)
            {
                int l=i,r=i+len-1;
                ans+=ve[r]-ve[l-1];
            }
        }
        return ans;
    }
};

以上是关于1588. 所有奇数长度子数组的和前缀和的主要内容,如果未能解决你的问题,请参考以下文章