力扣560.和为K的子数组

力扣560.和为K的子数组

  • 求前缀和 存入哈希表

    • 遍历所有前缀和 加入哈希表之前先找前面值为 k - s[i]的数量
cpp 复制代码
  class Solution {
  public:
      int subarraySum(vector<int>& nums, int k) {
          int n = nums.size();
          vector<int> s(n+1);
          for(int i=0;i<n;i++) s[i+1] = s[i] + nums[i];
  
          int res=0;
          unordered_map<int,int> cnt;
          for(int sj:s)
          {
              res += cnt[sj-k];
              cnt[sj] ++;
          }
          return res;
      }
  };
相关推荐
Swift社区3 小时前
LeetCode 447 - 回旋镖的数量
linux·算法·leetcode
java修仙传3 小时前
力扣hot100:路径总和III
数据结构·算法·leetcode
lang201509284 小时前
Sentinel黑白名单授权控制详解
java·算法·sentinel
hweiyu004 小时前
数据结构:加权图
数据结构
青蛙大侠公主4 小时前
为什么redis使用跳表mysql使用b+树:几种搜索的数据结构的对比
数据结构
leoufung4 小时前
题目介绍:LeetCode 79. Word Search
leetcode·word·深度优先
小O的算法实验室4 小时前
2023年IEEE TIV,GA-LNS算法+直升机救援调度,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
foundbug9994 小时前
Delta并联机器人正逆解实现
算法·机器人
职业码农NO.14 小时前
《算法与数据结构》:最短路径
数据结构·算法
Ayanami_Reii4 小时前
进阶数据结构Splay应用-维护数列
数据结构·算法·splay·fhq