-
Notifications
You must be signed in to change notification settings - Fork 2
/
560.subarray-sum-equals-k.java
51 lines (41 loc) · 1.25 KB
/
560.subarray-sum-equals-k.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* @lc app=leetcode id=560 lang=java
*
* [560] Subarray Sum Equals K
*/
// @lc code=start
class Solution {
public int subarraySum2(int[] nums, int k) {
int[] prefixSum = new int[nums.length + 1];
prefixSum[0] = 0;
int count = 0;
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
for (int i = 1; i <= nums.length; i++) {
prefixSum[i] = prefixSum[i - 1] + nums[i - 1];
if (map.containsKey(prefixSum[i] - k)) {
count += map.get(prefixSum[i] - k);
}
map.put(prefixSum[i], map.getOrDefault(prefixSum[i], 0) + 1);
}
return count;
}
public int subarraySum(int[] nums, int k) {
int[] prefixSum = new int[nums.length + 1];
prefixSum[0] = 0;
int count = 0;
for (int i = 1; i <= nums.length; i++) {
prefixSum[i] = prefixSum[i - 1] + nums[i - 1];
}
for (int i = 0; i < nums.length; i++) {
for (int j = i; j < nums.length; j++) {
int currSum = prefixSum[j + 1] - prefixSum[i];
if (currSum == k) {
count++;
}
}
}
return count;
}
}
// @lc code=end