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,ve,奇数,int,sum,arr,len,ans,前缀 来源: https://blog.csdn.net/bettle_king/article/details/120645500