第一次忘记jump
第二次忘记了i++
..然后ac了
//413. Arithmetic Slices
//point at least three elements// 1 2 -> 3..n-2 -> n-1..n
//数组:来做的话 1...n-2
// 如果l>3的话 结果就是 n=6->sum n-2+.....1
//然后跳到n+l重新开始
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
int ax(int n){
return (n-2)*(n-2+1)/2;
}
int numberOfArithmeticSlices(vector<int>& A) {
int sum=0;
int n=A.size();
int j;
for(int i=0;i<n-2;i++){
for( j=i+2;j<n;j++){
if(A[j]-A[j-1]!=A[j-1]-A[j-2])
break;
}
sum+=ax(j-i);
//jump to j-1
i=j-2;//少算了i++
}
return sum;
}
};
int main(){
Solution a;
vector<int> b;
b.push_back(1);
b.push_back(1);
b.push_back(1);
b.push_back(1);
b.push_back(2);
b.push_back(3);
b.push_back(4);
cout<<a.numberOfArithmeticSlices(b);
}