C++ | Leetcode C++题解之第560题和为K的子数组

题目:

题解:

cpp 复制代码
class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        unordered_map<int, int> mp;
        mp[0] = 1;
        int count = 0, pre = 0;
        for (auto& x:nums) {
            pre += x;
            if (mp.find(pre - k) != mp.end()) {
                count += mp[pre - k];
            }
            mp[pre]++;
        }
        return count;
    }
};
相关推荐
程序员编程指南3 分钟前
Qt OpenGL 集成:开发 3D 图形应用
c语言·数据库·c++·qt·3d
小徐不徐说40 分钟前
动态规划:从入门到精通
数据结构·c++·算法·leetcode·动态规划·代理模式
jtymyxmz1 小时前
刷题日记0726
leetcode
小新学习屋1 小时前
《剑指offer》-数据结构篇-树
数据结构·算法·leetcode
程序员编程指南2 小时前
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