forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0560.cpp
More file actions
29 lines (28 loc) · 655 Bytes
/
0560.cpp
File metadata and controls
29 lines (28 loc) · 655 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int subarraySum(vector<int>& nums, int k)
{
int result = 0, cur_sum = 0;
unordered_map<int, int> sum_dict = {{0, 1}};
for (auto& num : nums)
{
cur_sum += num;
result += sum_dict[cur_sum - k];
sum_dict[cur_sum]++;
}
return result;
}
};
int main()
{
vector<int> nums = {1, 1, 1, 1, 1};
int k = 3;
cout << Solution().subarraySum(nums, k);
return 0;
}