美文网首页
413. Arithmetic Slices

413. Arithmetic Slices

作者: 阿团相信梦想都能实现 | 来源:发表于2016-12-14 11:58 被阅读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)

相关文章

网友评论

      本文标题:413. Arithmetic Slices

      本文链接:https://www.haomeiwen.com/subject/wteimttx.html