Skip to content

Commit 27848c9

Browse files
authored
Create Continuous Subarray Sum.java
1 parent a14f47a commit 27848c9

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Medium/Continuous Subarray Sum.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public boolean checkSubarraySum(int[] nums, int k) {
3+
Map<Integer, Integer> map = new HashMap<>();
4+
map.put(0, -1);
5+
int currSum = 0;
6+
for (int i = 0; i < nums.length; i++) {
7+
currSum += nums[i];
8+
int rem = currSum % k;
9+
if (map.containsKey(rem)) {
10+
if (i - map.get(rem) >= 2) {
11+
return true;
12+
}
13+
} else {
14+
map.put(rem, i);
15+
}
16+
}
17+
return false;
18+
}
19+
}

0 commit comments

Comments
 (0)