python leetcode 413. Arithmetic Slices

class Solution(object):
    def numberOfArithmeticSlices(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        res=0
        if len(A)<3:return 0
        pre=A[1]-A[0]
        index=0
        for i in range(2,len(A)):
            if pre!=A[i]-A[i-1]:
                if i-index>=3:res+=self.count(i-index)
                pre=A[i]-A[i-1]
                index=i-1 
        if i-index+1>=3:res+=self.count(i-index+1)
        return res 
    def count(self,n):
        return sum(i for i in range(1,n-1))

猜你喜欢

转载自blog.csdn.net/Neekity/article/details/84638996