LeetCode 413. Arithmetic Slices Posted on 2021-02-18 Edited on 2021-03-03 In LeetCode Notes 题目原题在此 解析 3个能构成1个Slices, 4个能构成1+2个, 5个能构成1+2+3个 每次比较三个就行, count每次比较成功就+1, 失败就归零. 代码c++1234567891011class Solution {public: int numberOfArithmeticSlices(vector<int>& A) { int n = A.size(), cnt = 0, res = 0; for(int i = 2; i < n; ++i){ if(A[i] - A[i - 1] == A[i - 1] - A[i - 2]) res += ++cnt; else cnt = 0; } return res; }};