413. Arithmetic Slices

2016-12-14  本文已影响0人  阿团相信梦想都能实现
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)
上一篇 下一篇

猜你喜欢

热点阅读