classSolution{public:intnumberOfArithmeticSlices(vector<int>&A){constintn=A.size();if(n<3)return0;vector<int>dp(n);// # arithmetic slices ends at ifor(inti=2;i<A.size();++i)if(A[i]-A[i-1]==A[i-1]-A[i-2])dp[i]=dp[i-1]+1;returnaccumulate(begin(dp),end(dp),0);}};
classSolution{publicintnumberOfArithmeticSlices(int[]A){finalintn=A.length;if(n<3)return0;int[]dp=newint[n];// dp[i] := # of arithmetic slices ends at A[i]for(inti=2;i<n;++i)if(A[i]-A[i-1]==A[i-1]-A[i-2])dp[i]+=dp[i-1]+1;returnArrays.stream(dp).sum();}}
classSolution{public:intnumberOfArithmeticSlices(vector<int>&A){intans=0;intdp=0;// # arithmetic slices ends at ifor(inti=2;i<A.size();++i)if(A[i]-A[i-1]==A[i-1]-A[i-2])ans+=++dp;elsedp=0;returnans;}};