446 Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

Posted lina2014

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了446 Arithmetic Slices II - Subsequence 算数切片之二 - 子序列相关的知识,希望对你有一定的参考价值。

详见:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/description/

C++:

class Solution {
public:
    int numberOfArithmeticSlices(vector<int>& A) 
    {
        int res = 0, n = A.size();
        vector<unordered_map<int, int>> dp(n);
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < i; ++j) 
            {
                long delta = (long)A[i] - A[j];
                if (delta > INT_MAX || delta < INT_MIN)
                {
                    continue;
                }
                int diff = (int)delta;
                ++dp[i][diff];
                if (dp[j].count(diff)) 
                {
                    res += dp[j][diff];
                    dp[i][diff] += dp[j][diff];
                }
            }
        }
        return res;
    }
};

 参考:https://www.cnblogs.com/grandyang/p/6057934.html

以上是关于446 Arithmetic Slices II - Subsequence 算数切片之二 - 子序列的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode446. Arithmetic Slices II - Subsequence

446 Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

Leetcode: Arithmetic Slices II - Subsequence

LeetCode Arithmetic Slices

arithmetic-slices

[LeetCode]413 Arithmetic Slices