class Solution(object):
def numberOfArithmeticSlices(self, A):
"""
:type A: List[int]
:rtype: int
"""
difference = None
sliceLength = 0
ret = 0
for i in range(1, len(A)):
if A[i]  A[i1] == difference:
sliceLength += 1
else:
if sliceLength >= 3:
ret += ((sliceLength  1) * (sliceLength  2)) / 2
sliceLength = 2
difference = A[i]  A[i1]
if sliceLength >= 3:
ret += ((sliceLength  1) * (sliceLength  2)) / 2
return ret
Python math solution


We're finding the number of different slices composed of 3 or more, so if a numeric slice is 3 long there is 1 slice, if its 4 long, there are 2 three length slices and one 4 length slices to get 3 total slices, and for 5 you get 1 + 2 + 3 = 6. For an n length slice, we are getting the sum from 1 to n  2, so I'm just putting n2 into the summation equation, which is (n * (n + 1)) / 2, to get ((n  2) * (n  1)) / 2. In hindsight, I could have just added n 2 to my return value every time, but this works as well.
