-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode560.java
More file actions
33 lines (27 loc) · 961 Bytes
/
leetcode560.java
File metadata and controls
33 lines (27 loc) · 961 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
import java.util.HashMap;
public class leetcode560 {
public static void main(String[] args) {
int[] num = new int[]{1,1,1};
Solution so = new Solution();
System.out.println(so.subarraySum(num,2));
}
static class Solution {
public int subarraySum(int[] nums, int k) {
int sum = 0;
if (nums == null || nums.length == 0) return 0;
//dp[i]表示前i个数的和
int[] dp = new int[nums.length + 1];
for (int i = 1; i <= nums.length; i++) {
dp[i] = dp[i - 1] + nums[i - 1];
}
int ret = 0;
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < dp.length; i++) {
if (map.containsKey(dp[i] - k))
ret += map.get(dp[i] - k);
map.put(dp[i], map.getOrDefault(dp[i], 0) + 1);
}
return ret;
}
}
}