β¨ Algorithm
- idea
- matching : two-sum
- count : hashMap
0 1 2 3 4 5
cur
cur-k
- Looking for sub-array which meets
k
. - Current value is
cur
.
const count = { 0: 1 };
let res = 0;
let cur = 0;
for (const num of nums) {
// do logic to change cur
if (obj[cur - k] !== undefined) res += obj[cur - k];
obj[cur] = (obj[cur] || 0) + 1;
}
return res;