C++ | Leetcode C++题解之第446题等差数列划分II-子序列

题目:

题解:

cpp 复制代码
class Solution {
public:
    int numberOfArithmeticSlices(vector<int> &nums) {
        int ans = 0;
        int n = nums.size();
        vector<unordered_map<long long, int>> f(n);
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < i; ++j) {
                long long d = 1LL * nums[i] - nums[j];
                auto it = f[j].find(d);
                int cnt = it == f[j].end() ? 0 : it->second;
                ans += cnt;
                f[i][d] += cnt + 1;
            }
        }
        return ans;
    }
};
相关推荐
小徐不徐说15 分钟前
动态规划:从入门到精通
数据结构·c++·算法·leetcode·动态规划·代理模式
jtymyxmz32 分钟前
刷题日记0726
leetcode
小新学习屋1 小时前
《剑指offer》-数据结构篇-树
数据结构·算法·leetcode
程序员编程指南1 小时前
Qt 网络编程进阶:RESTful API 调用
c语言·网络·c++·qt·restful
程序员编程指南2 小时前
Qt XML 与 JSON 数据处理方法
xml·c语言·c++·qt·json
Algebraaaaa3 小时前
【C++基础】指针常量 | 常量指针 | int* p | const int* p | int* const p| const int* const p
c++
祁同伟.3 小时前
【C++】类和对象(中)构造函数、析构函数
开发语言·c++
恣艺3 小时前
LeetCode 1074:元素和为目标值的子矩阵数量
算法·leetcode·矩阵
技术卷3 小时前
详解力扣高频SQL50题之1084. 销售分析 III【简单】
sql·leetcode·oracle
郝学胜-神的一滴4 小时前
C++ 类型萃取:深入理解与实践
开发语言·c++·程序人生