File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .util .*;
2+ class Solution {
3+ public int solution (int N , int number ) {
4+ int answer = 0 ;
5+ if (N == number ) {
6+ return 1 ;
7+ }
8+
9+ StringBuilder sb = new StringBuilder ();
10+
11+ List <Set <Integer >> list = new ArrayList <>();
12+
13+ for (int i = 0 ;i <= 8 ;i ++) {
14+ list .add (new HashSet <>());
15+ }
16+
17+ list .get (1 ).add (N );
18+
19+ sb .append (N );
20+
21+
22+ for (int i = 2 ; i <= 8 ;i ++) {
23+ sb .append (N );
24+ list .get (i ).add (Integer .parseInt (sb .toString ()));
25+
26+ for (int j = 1 ; j < i ;j ++) {
27+ int k = i - j ;
28+ for (int n1 : list .get (j )) {
29+ for (int n2 : list .get (k )) {
30+ list .get (i ).add (n1 + n2 );
31+ list .get (i ).add (n1 - n2 );
32+ list .get (i ).add (n1 * n2 );
33+ if (n2 != 0 ) {
34+ list .get (i ).add (n1 / n2 );
35+ }
36+ }
37+ }
38+ }
39+ if (list .get (i ).contains (number )) {
40+ return i ;
41+ }
42+
43+ }
44+
45+
46+
47+ return -1 ;
48+ }
49+ }
You can’t perform that action at this time.
0 commit comments