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

原文地址:https://www.cnblogs.com/xidian2014/p/8900622.html

时间: 2024-11-11 14:41:57

446 Arithmetic Slices II - Subsequence 算数切片之二 - 子序列的相关文章

LeetCode 446. Arithmetic Slices II - Subsequence

原题链接在这里:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 题目: A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, thes

Arithmetic Slices II - Subsequence LT446

446. Arithmetic Slices II - Subsequence Hard A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequences: 1,

Leetcode: Arithmetic Slices II - Subsequence

A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequences: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The follo

LeetCode446. Arithmetic Slices II - Subsequence

A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequences: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The follo

[LeetCode] Arithmetic Slices 算数切片

A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The followi

413. 数组切片 Arithmetic Slices

A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The followi

413. Arithmetic Slices

A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The followi

LeetCode - 413. Arithmetic Slices - 含中文题意解释 - O(n) - ( C++ ) - 解题报告

1.题目大意 A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The

413. Arithmetic Slices(LeetCode)

A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The followi