풀이
class Solution {
public int subarraySum(int[] nums, int k) {
Map<Integer, Integer> hashMap = new HashMap<>();
int curr = 0;
int answer = 0;
hashMap.put(0,1);
for(int i=0;i<nums.length;i++) {
curr += nums[i];
answer += hashMap.getOrDefault(curr-k,0);
hashMap.put(curr, hashMap.getOrDefault(curr, 0)+1);
}
return answer;
}
}
'Software Engineering > Algorithm' 카테고리의 다른 글
[LeetCode] #1248 Count Number of Nice Subarrays (0) | 2024.08.05 |
---|---|
[LeetCode] #1941 Check if All Characters Have Equal Number of Occurrences (0) | 2024.06.23 |
[LeetCode] #2248 Intersection of Multiple Arrays (0) | 2024.06.23 |
[LeetCode] First Letter to Appear Twice (1) | 2024.06.22 |
[LeetCode] Two Sum (0) | 2024.06.22 |