码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-04-21 13:31:43      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:for   bsp   ++   html   子序列   c++   com   turn   cto   

详见: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 算数切片之二 - 子序列

标签:for   bsp   ++   html   子序列   c++   com   turn   cto   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!