forked from BigEggStudy/LeetCode-CS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0560-SubarraySumEqualsK.cs
More file actions
35 lines (30 loc) · 935 Bytes
/
0560-SubarraySumEqualsK.cs
File metadata and controls
35 lines (30 loc) · 935 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
//-----------------------------------------------------------------------------
// Runtime: 104ms
// Memory Usage: 30.1 MB
// Link: https://leetcode.com/submissions/detail/260821531/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0560_SubarraySumEqualsK
{
public int SubarraySum(int[] nums, int k)
{
var map = new Dictionary<int, int>();
map.Add(0, 1);
var sum = 0;
var count = 0;
for (int i = 0; i < nums.Length; i++)
{
sum += nums[i];
if (map.ContainsKey(sum - k))
count += map[sum - k];
if (map.ContainsKey(sum))
map[sum] += 1;
else
map[sum] = 1;
}
return count;
}
}
}