都是连续的 数组才 ++
动态规划 注意边界值
class Solution(object):
def numberOfArithmeticSlices(self, A):
sizeof_input = len(A)
if sizeof_input < 3:
return 0
dp = [0] * sizeof_input
for index in range(2, sizeof_input):
if A[index] - A[index-1] == A[index-1] - A[index-2]:
dp[index] = dp[index-1] + 1
return sum(dp)
def test_module():
func = Solution().numberOfArithmeticSlices
assert(func([1, 2, 3, 4]) == 3)
assert(func([1,2]) == 0)
assert(func([1,2,3]) == 1)
assert(func([1,2,0]) == 0)
assert(func([1, 2, 3, 5]) == 1)
test_module()