Many ways to do, using dp, we can let dp[i] be the number of slices until i in the array, so the state transformation is dp[i]=dp[i-1]+1, when the new number is able to make a arithmetic slice with before. The total number is to add numbers of each position until the last element.
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
int l = A.size();
vector<int> dp(l);
int num = 0;
for(int i=0;i<l;i++){
dp[i]=0;
}
for(int i=2;i<l;i++){
if(A[i-1]-A[i-2]==A[i]-A[i-1]){
dp[i] = dp[i-1]+1;
}
num += dp[i];
}
return num;
}
};