-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
30 lines (29 loc) · 806 Bytes
/
Solution.cs
File metadata and controls
30 lines (29 loc) · 806 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
namespace LeetCode.Medium.Problem560{
//560. Subarray Sum Equals K
//https://leetcode.com/problems/subarray-sum-equals-k
/*
Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k.
A subarray is a contiguous non-empty sequence of elements within an array.
*/
public class Solution {
public int SubarraySum(int[] nums, int k) {
Dictionary <int,int> dic = new Dictionary <int,int>();
int ans = 0;
int sum = 0;
dic.Add(0,1);
for (int i = 0; i < nums.Length; i++)
{
sum += nums[i];
if (dic.ContainsKey(sum - k))
ans += dic[sum-k];
if (dic.ContainsKey(sum)) {
dic[sum] += 1;
}
else {
dic.Add(sum, 1);
}
}
return ans;
}
}
}