File tree Expand file tree Collapse file tree 2 files changed +16
-8
lines changed Expand file tree Collapse file tree 2 files changed +16
-8
lines changed Original file line number Diff line number Diff line change 68
68
* @return {boolean}
69
69
*/
70
70
const judgePoint24 = function (cards ) {
71
- if (cards .length === 1 ) return Math .abs (cards[0 ] - 24 ) < 1e-6 ;
72
-
73
71
const n = cards .length ;
74
72
73
+ if (n === 1 ) return Math .abs (cards[0 ] - 24 ) < 10e-9 ;
74
+
75
75
const getValues = (a , b ) => {
76
76
const values = [a + b, a * b, a - b, b - a];
77
77
78
78
if (a) values .push (b / a);
79
79
if (b) values .push (a / b);
80
+
80
81
return values;
81
82
};
82
83
83
- for (let a = 1 ; a < n; a++ ) {
84
- for (let b = 0 ; b < a ; b++ ) {
84
+ for (let a = 0 ; a < n - 1 ; a++ ) {
85
+ for (let b = a + 1 ; b < n ; b++ ) {
85
86
const values = getValues (cards[a], cards[b]);
86
87
const nextCards = cards .filter ((_ , index ) => index !== a && index !== b);
87
88
88
89
for (const value of values) {
89
90
nextCards .push (value);
91
+
90
92
if (judgePoint24 (nextCards)) return true ;
93
+
91
94
nextCards .pop ();
92
95
}
93
96
}
94
97
}
98
+
95
99
return false ;
96
100
};
97
101
```
Original file line number Diff line number Diff line change 3
3
* @return {boolean }
4
4
*/
5
5
const judgePoint24 = function ( cards ) {
6
- if ( cards . length === 1 ) return Math . abs ( cards [ 0 ] - 24 ) < 1e-6 ;
7
-
8
6
const n = cards . length ;
9
7
8
+ if ( n === 1 ) return Math . abs ( cards [ 0 ] - 24 ) < 10e-9 ;
9
+
10
10
const getValues = ( a , b ) => {
11
11
const values = [ a + b , a * b , a - b , b - a ] ;
12
12
13
13
if ( a ) values . push ( b / a ) ;
14
14
if ( b ) values . push ( a / b ) ;
15
+
15
16
return values ;
16
17
} ;
17
18
18
- for ( let a = 1 ; a < n ; a ++ ) {
19
- for ( let b = 0 ; b < a ; b ++ ) {
19
+ for ( let a = 0 ; a < n - 1 ; a ++ ) {
20
+ for ( let b = a + 1 ; b < n ; b ++ ) {
20
21
const values = getValues ( cards [ a ] , cards [ b ] ) ;
21
22
const nextCards = cards . filter ( ( _ , index ) => index !== a && index !== b ) ;
22
23
23
24
for ( const value of values ) {
24
25
nextCards . push ( value ) ;
26
+
25
27
if ( judgePoint24 ( nextCards ) ) return true ;
28
+
26
29
nextCards . pop ( ) ;
27
30
}
28
31
}
29
32
}
33
+
30
34
return false ;
31
35
} ;
You can’t perform that action at this time.
0 commit comments