forked from BigEggStudy/LeetCode-CS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0523-ContinuousSubarraySum.cs
More file actions
37 lines (32 loc) · 966 Bytes
/
0523-ContinuousSubarraySum.cs
File metadata and controls
37 lines (32 loc) · 966 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
36
37
//-----------------------------------------------------------------------------
// Runtime: 120ms
// Memory Usage: 32 MB
// Link: https://leetcode.com/submissions/detail/375092264/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0523_ContinuousSubarraySum
{
public bool CheckSubarraySum(int[] nums, int k)
{
var map = new Dictionary<int, int>();
map.Add(0, -1);
var sum = 0;
for (int i = 0; i < nums.Length; i++)
{
sum += nums[i];
if (k != 0)
sum %= k;
if (map.ContainsKey(sum))
{
if (i - map[sum] >= 2)
return true;
}
else
map[sum] = i;
}
return false;
}
}
}