-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithmetic-slices.cpp
More file actions
34 lines (34 loc) · 824 Bytes
/
arithmetic-slices.cpp
File metadata and controls
34 lines (34 loc) · 824 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
/*
0 1 2 3
*/
public:
int numberOfArithmeticSlices(vector<int>& A) {
if(A.size()<3) return 0;
vector<int> result(A.size(),0);
int index=0;
while(index<=A.size()-1-2){
int d=A[index+1]-A[index];
int x=index+2;
while(A[x]-A[x-1]==d) x++;
x--;
if(x-index>=2){
result[index]=x;
index=x+1;
cout<<"success_x="<<x<<endl;
}
else{
cout<<"fail_x="<<x<<endl;
index++;
}
}
int count=0;
for(int i=0;i<result.size();i++){
int len=result[i]-i+1;
if(len>2){
count+=(len - 1) * (len - 2) * 0.5;
}
}
return count;
}
};