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;
    }
};
相关推荐
AA陈超10 分钟前
虚幻引擎5 GAS开发俯视角RPG游戏 P06-09 玩家等级与战斗接口
c++·游戏·ue5·游戏引擎·虚幻
·白小白25 分钟前
力扣(LeetCode) ——118.杨辉三角(C++)
c++·算法·leetcode
tongsound29 分钟前
libmodbus 使用示例
linux·c++
sulikey1 小时前
C++的STL:深入理解 C++ 的 std::initializer_list
开发语言·c++·stl·list·initializerlist·c++标准库
代大大1 小时前
sciter.js 之cpp使用教程(1)
c++·前端框架
仰泳的熊猫1 小时前
LeetCode:207. 课程表
数据结构·c++·算法·leetcode
liu****1 小时前
19.map和set的封装
开发语言·数据结构·c++·算法
孤廖1 小时前
C++ 模板再升级:非类型参数、特化技巧(含全特化与偏特化)、分离编译破解
linux·服务器·开发语言·c++·人工智能·后端·深度学习
水冗水孚1 小时前
双指针算法在实际开发中的具体应用之代码Review文章字符串的片段分割
算法·leetcode
田野追逐星光1 小时前
STL中容器string -- 讲解超详细
c++