-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion4.java
More file actions
29 lines (25 loc) · 972 Bytes
/
Copy pathQuestion4.java
File metadata and controls
29 lines (25 loc) · 972 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package practice2_hashing;
import java.util.*;
public class Question4 {
// 음수가 있는 부분 수열.. 진짜 개어렵네..?
public int solution(int[] nums, int m){
int answer = 0;
HashMap<Integer, Integer> nH = new HashMap<>();
int sum = 0;
nH.put(0, 1);
for(int x : nums){
sum += x;
if(nH.containsKey(sum-m)) answer += nH.get(sum-m);
nH.put(sum, nH.getOrDefault(sum, 0) + 1);
}
return answer;
}
public static void main(String[] args){
Question4 T = new Question4();
System.out.println(T.solution(new int[]{2, 2, 3, -1, -1, -1, 3, 1, 1}, 5));
System.out.println(T.solution(new int[]{1, 2, 3, -3, 1, 2, 2, -3}, 5));
System.out.println(T.solution(new int[]{1, 2, 3, -3, 1, 2}, 3));
System.out.println(T.solution(new int[]{-1, 0, 1}, 0));
System.out.println(T.solution(new int[]{-1, -1, -1, 1}, 0));
}
}