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;
    }
};
相关推荐
西阳未落1 小时前
LeetCode——二分(进阶)
算法·leetcode·职场和发展
FFZero11 小时前
【C++/Lua联合开发】 (二) Lua调用C++函数
c++·junit·lua
CoderCodingNo2 小时前
【GESP】C++四级真题 luogu-B4068 [GESP202412 四级] Recamán
开发语言·c++·算法
一个不知名程序员www2 小时前
算法学习入门---双指针(C++)
c++·算法
Maple_land2 小时前
常见Linux环境变量深度解析
linux·运维·服务器·c++·centos
Larry_Yanan2 小时前
QML学习笔记(四十四)QML与C++交互:对QML对象设置objectName
开发语言·c++·笔记·qt·学习·ui·交互
Want5953 小时前
C/C++大雪纷飞①
c语言·开发语言·c++
Mr_WangAndy3 小时前
C++设计模式_行为型模式_策略模式Strategy
c++·设计模式·策略模式·依赖倒置原则
吃着火锅x唱着歌3 小时前
LeetCode 410.分割数组的最大值
数据结构·算法·leetcode
LoveXming3 小时前
Chapter11—适配器模式
c++·设计模式·适配器模式·开闭原则