File tree Expand file tree Collapse file tree 2 files changed +17
-6
lines changed
solution/0500-0599/0564.Find the Closest Palindrome Expand file tree Collapse file tree 2 files changed +17
-6
lines changed Original file line number Diff line number Diff line change @@ -188,6 +188,7 @@ func abs(x int) int {
188
188
```
189
189
190
190
#### JavaScript
191
+
191
192
``` js
192
193
/**
193
194
* @param {string} n
@@ -216,13 +217,19 @@ function getCandidates(n) {
216
217
const res = new Set ();
217
218
218
219
res .add (BigInt (Math .pow (10 , length - 1 ) - 1 ));
219
- res .add (BigInt (Math .pow (10 , length) + 1 ));
220
+ res .add (BigInt (Math .pow (10 , length) + 1 ));
220
221
221
222
const left = BigInt (n .substring (0 , Math .ceil (length / 2 )));
222
223
223
224
for (let i = left - 1n ; i <= left + 1n ; i++ ) {
224
225
const prefix = i .toString ();
225
- const t = prefix + prefix .split (' ' ).reverse ().slice (length % 2 ).join (' ' );
226
+ const t =
227
+ prefix +
228
+ prefix
229
+ .split (' ' )
230
+ .reverse ()
231
+ .slice (length % 2 )
232
+ .join (' ' );
226
233
res .add (BigInt (t));
227
234
}
228
235
@@ -233,7 +240,6 @@ function getCandidates(n) {
233
240
function absDiff (a , b ) {
234
241
return a > b ? a - b : b - a;
235
242
}
236
-
237
243
```
238
244
239
245
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -25,13 +25,19 @@ function getCandidates(n) {
25
25
const res = new Set ( ) ;
26
26
27
27
res . add ( BigInt ( Math . pow ( 10 , length - 1 ) - 1 ) ) ;
28
- res . add ( BigInt ( Math . pow ( 10 , length ) + 1 ) ) ;
28
+ res . add ( BigInt ( Math . pow ( 10 , length ) + 1 ) ) ;
29
29
30
30
const left = BigInt ( n . substring ( 0 , Math . ceil ( length / 2 ) ) ) ;
31
31
32
32
for ( let i = left - 1n ; i <= left + 1n ; i ++ ) {
33
33
const prefix = i . toString ( ) ;
34
- const t = prefix + prefix . split ( '' ) . reverse ( ) . slice ( length % 2 ) . join ( '' ) ;
34
+ const t =
35
+ prefix +
36
+ prefix
37
+ . split ( '' )
38
+ . reverse ( )
39
+ . slice ( length % 2 )
40
+ . join ( '' ) ;
35
41
res . add ( BigInt ( t ) ) ;
36
42
}
37
43
@@ -42,4 +48,3 @@ function getCandidates(n) {
42
48
function absDiff ( a , b ) {
43
49
return a > b ? a - b : b - a ;
44
50
}
45
-
You can’t perform that action at this time.
0 commit comments