class Solution {
static int[] keys = new int[32768];
static int[] values = new int[32768];
public int subarraySum(int[] nums, int k) {
Arrays.fill(keys, Integer.MAX_VALUE);
Arrays.fill(values, 0);
int sum = 0, res = 0;
for (int i = 0; i < nums.length; i++) {
nums[i] += sum;
sum = nums[i];
int key = sum - k;
int hash = key & 0x7fff;
while (keys[hash] != key && keys[hash] != Integer.MAX_VALUE) {
++hash;
if (hash == keys.length) {
hash = 0;
}
}
res += values[hash];
if (sum == k) {
++res;
}
key = sum;
hash = key & 0x7fff;
while (keys[hash] != key && keys[hash] != Integer.MAX_VALUE) {
++hash;
if (hash == keys.length) {
hash = 0;
}
}
keys[hash] = key;
values[hash]++;
}
return res;
}
}