class Solution {
public int subarraySum(int[] nums, int k) {
int[] dp = new int[nums.length+1];
dp[0] = 0;
for(int i = 0;i<nums.length;i++){
dp[i+1] = dp[i] + nums[i];
}
int count = 0;
for(int j = 0;j<nums.length;j++){
for(int m = j+1;m<nums.length+1;m++){
int sum = dp[m] - dp[j];
if(sum == k){
count++;
}
}
}
return count;
}
}
网友评论