Created
January 28, 2019 17:09
-
-
Save Anirudhk94/20dccee5c57b35d90da5125e2863c18e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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