Leetcode-413 Arithmetic Slices(等差数列划分)
作者:互联网
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 };
标签:Slices,int,sum,len,413,numberOfArithmeticSlices,Arithmetic,size 来源: https://www.cnblogs.com/Asurudo/p/10351668.html