-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_0560_subarraySum.cc
More file actions
57 lines (52 loc) · 954 Bytes
/
Problem_0560_subarraySum.cc
File metadata and controls
57 lines (52 loc) · 954 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <unordered_map>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
int subarraySum(vector<int> &nums, int k)
{
if (nums.size() == 0)
{
return 0;
}
unordered_map<int, int> map;
// 为了让每个单独的元素可以满足条件
map.emplace(0, 1);
int all = 0;
int ans = 0;
for (int i = 0; i < nums.size(); i++)
{
all += nums[i]; // 0...i的前缀和
if (map.count(all - k))
{
ans += map.at(all - k);
}
if (!map.count(all))
{
map.emplace(all, 1);
}
else
{
map[all]++;
}
}
return ans;
}
};
void testSubArraySum()
{
Solution s;
vector<int> n1 = {1, 1, 1};
vector<int> n2 = {1, 2, 3};
EXPECT_EQ_INT(2, s.subarraySum(n1, 2));
EXPECT_EQ_INT(2, s.subarraySum(n2, 3));
EXPECT_SUMMARY;
}
int main()
{
testSubArraySum();
return 0;
}