From 124b24c36164677109f059201d0b0e6975ebf1a7 Mon Sep 17 00:00:00 2001 From: xingyou wu <3050128610@qq.com> Date: Fri, 12 Sep 2025 00:42:49 +0800 Subject: [PATCH] =?UTF-8?q?560.=20=E5=92=8C=E4=B8=BA=20K=20=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test010.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 test010.cpp diff --git a/test010.cpp b/test010.cpp new file mode 100644 index 0000000..db70ee3 --- /dev/null +++ b/test010.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int subarraySum(vector& nums, int k) { + int n = nums.size(); + vector p(n+1, 0); + for(int i = 1; i <= n; i++){ + p[i] = p[i - 1] + nums[i - 1]; + } + int ret = 0; + unordered_map data; + for(int l = n; l; l--){ + data[p[l]]++; + int target = k + p[l-1]; + if(data.count(target)){ + ret += data[target]; + } + } + return ret; + } +}; \ No newline at end of file