Skip to content

Commit 25b8eb7

Browse files
authored
Create Solution.java
1 parent 3475c00 commit 25b8eb7

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int subarraySum(int[] nums, int k) {
3+
Map<Integer, Integer> map = new HashMap<>();
4+
map.put(0, 1);
5+
int res = 0;
6+
int s = 0;
7+
for (int i = 0; i < nums.length; ++i) {
8+
s += nums[i];
9+
res += map.getOrDefault(s - k, 0);
10+
map.put(s, map.getOrDefault(s, 0) + 1);
11+
}
12+
return res;
13+
}
14+
}

0 commit comments

Comments
 (0)