python DP solution


  • 0
    F

    class Solution(object):
    def numberOfArithmeticSlices(self, A):
    """
    :type A: List[int]
    :rtype: int
    """

        #1st
        def helper(B, e):       #judge whether array B , plus a new element, constructing an arithmetic slice
            #print ('B: ',B, e)
            if (B[-2] - B[-1] == B[-1] - e):
                return True
            else:
                return False
            
        count = 0
        for i in range(0, len(A)):
            j = i + 2
            
            while j < len(A):
                if helper(A[i:j], A[j]):
                    count += 1
                    j += 1
                else:
                    break
        return count

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.