413. Arithmetic Slices
思路:动态规划 参考:
解法一:
class Solution { public: int numberOfArithmeticSlices(vector<int>& A) { int res = 0, len = 2, n = A.size(); for (int i = 2; i < n; ++i) { if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) { ++len; } else { if (len > 2) res += (len - 1) * (len - 2) * 0.5; len = 2; } } if (len > 2) res += (len - 1) * (len - 2) * 0.5; return res; } };
解法二:
class Solution { public: int numberOfArithmeticSlices(vector<int>& A) { int res = 0, n = A.size(); vector<int> dp(n, 0); for (int i = 2; i < n; ++i) { if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) { dp[i] = dp[i - 1] + 1; } res += dp[i]; } return res; } };