|
1 | 1 | import java.util.*; |
2 | 2 |
|
3 | 3 | class Solution { |
| 4 | + ArrayList<HashSet<Integer>> dp = new ArrayList<>(); |
4 | 5 |
|
5 | 6 | public int solution(int N, int number) { |
6 | | - ArrayList<HashSet<Integer>> dp = new ArrayList<>(); |
7 | | - HashSet<Integer> set = new HashSet<>(); |
| 7 | + if(N == number) return 1; |
| 8 | + // 빈 리스트 생성 |
| 9 | + for(int i = 0; i <= 8; i++){ |
| 10 | + dp.add(new HashSet<Integer>()); |
| 11 | + } |
8 | 12 |
|
9 | | - for(int i = 0; i < 9; i ++) { |
10 | | - dp.add(new HashSet<>()); |
| 13 | + // 이어 붙이기 |
| 14 | + StringBuilder sb = new StringBuilder(); |
| 15 | + for(int i = 1; i <= 8; i++) { |
| 16 | + sb.append(N); |
| 17 | + dp.get(i).add(Integer.parseInt(sb.toString())); |
11 | 18 | } |
12 | 19 |
|
13 | | - for(int i = 1; i < 9; i++){ |
| 20 | + // 사칙 연산 |
| 21 | + for(int i = 2; i <= 8; i++) { |
14 | 22 | HashSet<Integer> currentSet = dp.get(i); |
15 | | - StringBuilder sb = new StringBuilder(); |
16 | | - for(int j = 0; j < i; j ++){ |
17 | | - sb.append(N); |
18 | | - } |
19 | | - |
20 | | - currentSet.add(Integer.parseInt(sb.toString())); |
21 | 23 |
|
22 | | - // 사칙연산 |
23 | 24 | for(int j = 1; j < i; j++) { |
| 25 | + int k = i - j; |
24 | 26 | HashSet<Integer> setJ = dp.get(j); |
25 | | - HashSet<Integer> setK = dp.get(i-j); |
| 27 | + HashSet<Integer> setK = dp.get(k); |
26 | 28 |
|
27 | | - for(int num1 : setJ){ |
28 | | - for(int num2 : setK){ |
| 29 | + for(Integer num1 : setJ) { |
| 30 | + for(Integer num2 : setK) { |
29 | 31 | currentSet.add(num1 + num2); |
30 | | - currentSet.add(num1 - num2); |
31 | | - currentSet.add(num1 * num2); |
32 | | - if(num2 != 0) currentSet.add(num1 / num2); |
| 32 | + currentSet.add(num1 - num2); |
| 33 | + currentSet.add(num1 * num2); |
| 34 | + if(num2 != 0) currentSet.add(num1 / num2); |
33 | 35 | } |
34 | 36 | } |
35 | 37 | } |
|
0 commit comments