446 Arithmetic Slices II - Subsequence 算數切片之二 - 子序列
阿新 • • 發佈:2018-04-21
highlight seq www code nbsp true 序列 || map
詳見: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 算數切片之二 - 子序列