1 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 2 class Solution 3 { 4 public: 5 int f(int len) 6 { 7 int sum = 0; 8 len -= 2; 9 _for(i,1,len+1) 10 sum += i; 11 return sum; 12 } 13 int numberOfArithmeticSlices(vector<int>& A) 14 { 15 int cur = 0; 16 if(A.size()<2) 17 return 0; 18 19 int rnt = 0; 20 _for(i,0,A.size()) 21 { 22 if(!cur&&A[i]-A[i+1]==A[i+1]-A[i+2]) 23 { 24 cur = 3; 25 i += 2; 26 } 27 else if(cur&&A[i]-A[i-1]==A[i-1]-A[i-2]) 28 cur ++; 29 else 30 { 31 rnt += f(cur); 32 cur = 0; 33 if(!cur&&A[i]-A[i+1]==A[i+1]-A[i+2]) 34 { 35 cur = 3; 36 i += 2; 37 } 38 } 39 } 40 rnt += f(cur); 41 return rnt; 42 } 43 };
原文地址:https://www.cnblogs.com/Asurudo/p/10351668.html
时间: 2024-10-04 17:03:37