560. 和为 K 的子数组

560. 和为 K 的子数组


题目链接:560. 和为 K 的子数组

代码如下:

cpp 复制代码
class Solution 
{
public:
    int subarraySum(vector<int>& nums, int k) 
    {
        unordered_map<int,int> um;
        um[0]=1;
        int res=0,pre=0;
        for(int i=0;i<nums.size();i++)
        {
            pre+=nums[i];
            if(um.find(pre-k)!=um.end())//找到了
            {
                res+=um[pre-k];
            }
            um[pre]++;
        }
        return res;
    }
};
相关推荐
Teleger43 分钟前
在window上使用c++控制鼠标点击,实现的exe
c++·单片机·计算机外设
June`2 小时前
高并发内存池如何实现
c++·tcmalloc·内存池
ComputerInBook2 小时前
C++ 关键字 constexpr 和 consteval 之注意事项
开发语言·c++·constexpr·consteval
米啦啦.2 小时前
STL(标准模板库)
开发语言·c++·stl
咩咦2 小时前
C++学习笔记08:指针和引用的区别
c++·学习笔记·指针·引用·指针和引用
洛水水2 小时前
【力扣100题】34.二叉搜索树中第K小的元素
c++·算法·leetcode
许长安3 小时前
gRPC Keepalive 机制
c++·经验分享·笔记·rpc
wangjialelele3 小时前
Linux SystemV 消息队列 + 责任链模式:实现客户端消息处理流水线
linux·服务器·c语言·网络·c++·责任链模式
智者知已应修善业3 小时前
51单片机4按键控制共阳LED霓虹灯切换1整体闪烁2流水下3流水上4间隔闪烁】2023-10-27
c++·经验分享·笔记·算法·51单片机
洛水水4 小时前
结构性设计模式详解
c++·设计模式