413. 等差数列划分
为保证权益,题目请参考 413. 等差数列划分(From LeetCode).
解决方案1
Python
python
# 413. 等差数列划分
# https://leetcode-cn.com/problems/arithmetic-slices/
from typing import List
class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
if len(nums) < 3:
return 0
d = nums[1] - nums[0]
tn = 0
ans = 0
for i in range(2, len(nums)):
t = nums[i] - nums[i - 1]
if d == t:
tn += 1
else:
d = t
tn = 0
ans += tn
return ans
if __name__ == "__main__":
solution = Solution()
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
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