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

上一篇:Android高效加载大图、多图解决方案,有效避免程序OOM


下一篇:Entity Framework入门教程(12)--- EF进行批量添加/删除