-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubarray_sum_equals_k.cpp
More file actions
90 lines (81 loc) · 2.03 KB
/
subarray_sum_equals_k.cpp
File metadata and controls
90 lines (81 loc) · 2.03 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* =====================================================================================
*
* Filename: subarray_sum_equals_k.cpp
*
* Description: 560. Subarray Sum Equals K.
* Given an array of integers and an integer k, you need to find the
* total number of continuous subarrays whose sum equals to k.
*
* Version: 1.0
* Created: 04/30/19 01:47:51
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include<unordered_map>
class Solution1
{
public:
int subarraySum(std::vector<int>& nums, int k)
{
int sum = 0;
for (int i = 1; i < nums.size(); i++)
{
nums[i] += nums[i - 1];
}
for (int i = 0; i < nums.size(); i++)
{
for (int j = i; j < nums.size(); j++)
{
if (nums[j] == k)
{
sum++;
}
if (j > i)
{
nums[j] -= nums[i];
}
}
}
return sum;
}
};
class Solution2
{
public:
int subarraySum(std::vector<int>& nums, int k)
{
std::unordered_map<int, int> pre_sums = {{0, 1},};
int sum = 0;
int cnt = 0;
for (int i = 0; i < nums.size(); i++)
{
sum += nums[i];
int sub = sum - k;
auto it = pre_sums.find(sub);
if (it != pre_sums.end())
{
cnt += it->second;
}
pre_sums[sum]++;
}
return cnt;
}
};
using Solution = Solution2;
int main(int argc, char* argv[])
{
std::vector<int> nums = {1, 2, 1, 2, 1};
int k = 3;
int sum = Solution().subarraySum(nums, k);
printf("Total number of continuous subarrays: %d\n", sum);
return 0;
}