forked from LeetCode-in-Net/LeetCode-in-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
28 lines (25 loc) · 891 Bytes
/
Solution.cs
File metadata and controls
28 lines (25 loc) · 891 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
namespace LeetCodeNet.G0501_0600.S0560_subarray_sum_equals_k {
// #Medium #Top_100_Liked_Questions #Array #Hash_Table #Prefix_Sum #Data_Structure_II_Day_5_Array
// #Big_O_Time_O(n)_Space_O(n) #2025_06_16_Time_12_ms_(90.27%)_Space_52.19_MB_(67.86%)
using System.Collections.Generic;
public class Solution {
public int SubarraySum(int[] nums, int k) {
int tempSum = 0;
int ret = 0;
Dictionary<int, int> sumCount = new Dictionary<int, int>();
sumCount[0] = 1;
foreach (int i in nums) {
tempSum += i;
if (sumCount.ContainsKey(tempSum - k)) {
ret += sumCount[tempSum - k];
}
if (sumCount.ContainsKey(tempSum)) {
sumCount[tempSum] = sumCount[tempSum] + 1;
} else {
sumCount[tempSum] = 1;
}
}
return ret;
}
}
}