413. Arithmetic Slices

class Solution(object):
    def numberOfArithmeticSlices(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        n=len(A)
        if n<3:return 0
        #dp[i] is number of arithmetic slices ending with A[i]
        dp=[0]*n
        
        for i in xrange(2,n):
            if (A[i]-A[i-1]==A[i-1]-A[i-2]):
                dp[i]=dp[i-1]+1
        
                
        return sum(dp)

你可能感兴趣的:(413. Arithmetic Slices)