Skip to content

Instantly share code, notes, and snippets.

@Anirudhk94
Created January 28, 2019 17:09
Show Gist options
  • Save Anirudhk94/20dccee5c57b35d90da5125e2863c18e to your computer and use it in GitHub Desktop.
Save Anirudhk94/20dccee5c57b35d90da5125e2863c18e to your computer and use it in GitHub Desktop.
class Solution {
public int subarraySum(int[] nums, int k) {
int sum = 0;
int result = 0;
HashMap<Integer, Integer> prevSum = new HashMap<Integer, Integer>();
prevSum.put(0, 1);
for(int i = 0 ; i < nums.length ; i++) {
sum += nums[i];
if(prevSum.containsKey(sum - k))
result += prevSum.get(sum - k);
prevSum.put(sum, prevSum.getOrDefault(sum, 0) + 1);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment