-
Notifications
You must be signed in to change notification settings - Fork 4
/
arithmetic_slices.py
57 lines (40 loc) · 1.26 KB
/
arithmetic_slices.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
413. 等差数列划分
数组 动态规划
中等
如果一个数列 至少有三个元素 ,并且任意两个相邻元素之差相同,则称该数列为等差数列。
例如,[1,3,5,7,9]、[7,7,7,7] 和 [3,-1,-5,-9] 都是等差数列。
给你一个整数数组 nums ,返回数组 nums 中所有为等差数组的 子数组 个数。
子数组 是数组中的一个连续序列。
示例 1:
输入:nums = [1,2,3,4]
输出:3
解释:nums 中有三个子等差数组:[1, 2, 3]、[2, 3, 4] 和 [1,2,3,4] 自身。
示例 2:
输入:nums = [1]
输出:0
提示:
1 <= nums.length <= 5000
-1000 <= nums[i] <= 1000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/arithmetic-slices
"""
from typing import List
class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
n = len(nums)
dp = [0]*n
for i in range(1, n-1):
if nums[i-1] + nums[i+1] == nums[i]*2:
dp[i] = dp[i-1]+1
return sum(dp)
if __name__ == '__main__':
solution = Solution()
result = solution.numberOfArithmeticSlices([1, 2, 3, 4])
print(result)
assert result == 3
result = solution.numberOfArithmeticSlices([1])
print(result)
assert result == 0