
g0501_0600.s0560_subarray_sum_equals_k.Solution.cs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
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) #2024_01_07_Time_135_ms_(46.56%)_Space_50.6_MB_(20.87%)
using System.Collections.Generic;
public class Solution {
public int SubarraySum(int[] nums, int k) {
int tempSum = 0;
int ret = 0;
Dictionary sumCount = new Dictionary();
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;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy