0446.cpp (524B)
1 class Solution { 2 public: 3 int numberOfArithmeticSlices(const vector<int> &nums) const { 4 vector<unordered_map<long long, int>> dp(size(nums)); 5 int res = 0; 6 for (int i = 1; i < size(nums); i++) { 7 for (int j = 0; j < i; j++) { 8 const long long diff = (long long)nums[i] - nums[j]; 9 const int cnt = dp[j].count(diff) ? dp[j][diff] : 0; 10 dp[i][diff] += cnt + 1; 11 res += cnt; 12 } 13 } 14 return res; 15 } 16 };