-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path560.js
51 lines (44 loc) · 1.09 KB
/
560.js
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
// Using Cumulative Sum
var subarraySum = function(nums, k) {
let sum = [];
let count=0;
for(let i=0;i<=nums.length;i++){
if(i===0)sum[i] = 0;
else{sum[i]=sum[i-1]+nums[i-1];}
}
for(let start = 0;start<=nums.length;start++){
for(let end = start+1; end<=nums.length;end++){
// console.log(end,start)
if(sum[end]-sum[start]===k)count++;
}
}
return count;
};
// Without Space
var subarraySum = function(nums, k) {
let count=0;
for(let start = 0;start<nums.length;start++){
let sum = 0;
for(let end = start ; end<nums.length;end++){
sum+=nums[end];
if(sum===k)count++;
}
}
return count;
};
// Using Hashmap
var subarraySum = function(nums, k) {
let count=0;
let sum = 0;
let map = new Map();
map.set(0,1);
for(let i=0;i<nums.length;i++){
sum+=nums[i];
if(map.has(sum-k)){
count+=map.get(sum-k);
}
let temp = map.has(sum)?map.get(sum):0;
map.set(sum,temp+1);
}
return count;
};