File tree 3 files changed +160
-0
lines changed
solution/0500-0599/0564.Find the Closest Palindrome
3 files changed +160
-0
lines changed Original file line number Diff line number Diff line change @@ -189,6 +189,61 @@ func abs(x int) int {
189
189
}
190
190
```
191
191
192
+ #### JavaScript
193
+
194
+ ``` js
195
+ /**
196
+ * @param {string} n
197
+ * @return {string}
198
+ */
199
+
200
+ function nearestPalindromic (n ) {
201
+ const x = BigInt (n);
202
+ let ans = null ;
203
+
204
+ for (const t of getCandidates (n)) {
205
+ if (
206
+ ans === null ||
207
+ absDiff (t, x) < absDiff (ans, x) ||
208
+ (absDiff (t, x) === absDiff (ans, x) && t < ans)
209
+ ) {
210
+ ans = t;
211
+ }
212
+ }
213
+
214
+ return ans .toString ();
215
+ }
216
+
217
+ function getCandidates (n ) {
218
+ const length = n .length ;
219
+ const res = new Set ();
220
+
221
+ res .add (BigInt (Math .pow (10 , length - 1 ) - 1 ));
222
+ res .add (BigInt (Math .pow (10 , length) + 1 ));
223
+
224
+ const left = BigInt (n .substring (0 , Math .ceil (length / 2 )));
225
+
226
+ for (let i = left - 1n ; i <= left + 1n ; i++ ) {
227
+ const prefix = i .toString ();
228
+ const t =
229
+ prefix +
230
+ prefix
231
+ .split (' ' )
232
+ .reverse ()
233
+ .slice (length % 2 )
234
+ .join (' ' );
235
+ res .add (BigInt (t));
236
+ }
237
+
238
+ res .delete (BigInt (n));
239
+ return res;
240
+ }
241
+
242
+ function absDiff (a , b ) {
243
+ return a > b ? a - b : b - a;
244
+ }
245
+ ```
246
+
192
247
<!-- tabs:end -->
193
248
194
249
<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -187,6 +187,61 @@ func abs(x int) int {
187
187
}
188
188
```
189
189
190
+ #### JavaScript
191
+
192
+ ``` js
193
+ /**
194
+ * @param {string} n
195
+ * @return {string}
196
+ */
197
+
198
+ function nearestPalindromic (n ) {
199
+ const x = BigInt (n);
200
+ let ans = null ;
201
+
202
+ for (const t of getCandidates (n)) {
203
+ if (
204
+ ans === null ||
205
+ absDiff (t, x) < absDiff (ans, x) ||
206
+ (absDiff (t, x) === absDiff (ans, x) && t < ans)
207
+ ) {
208
+ ans = t;
209
+ }
210
+ }
211
+
212
+ return ans .toString ();
213
+ }
214
+
215
+ function getCandidates (n ) {
216
+ const length = n .length ;
217
+ const res = new Set ();
218
+
219
+ res .add (BigInt (Math .pow (10 , length - 1 ) - 1 ));
220
+ res .add (BigInt (Math .pow (10 , length) + 1 ));
221
+
222
+ const left = BigInt (n .substring (0 , Math .ceil (length / 2 )));
223
+
224
+ for (let i = left - 1n ; i <= left + 1n ; i++ ) {
225
+ const prefix = i .toString ();
226
+ const t =
227
+ prefix +
228
+ prefix
229
+ .split (' ' )
230
+ .reverse ()
231
+ .slice (length % 2 )
232
+ .join (' ' );
233
+ res .add (BigInt (t));
234
+ }
235
+
236
+ res .delete (BigInt (n));
237
+ return res;
238
+ }
239
+
240
+ function absDiff (a , b ) {
241
+ return a > b ? a - b : b - a;
242
+ }
243
+ ```
244
+
190
245
<!-- tabs:end -->
191
246
192
247
<!-- solution:end -->
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } n
3
+ * @return {string }
4
+ */
5
+
6
+ function nearestPalindromic ( n ) {
7
+ const x = BigInt ( n ) ;
8
+ let ans = null ;
9
+
10
+ for ( const t of getCandidates ( n ) ) {
11
+ if (
12
+ ans === null ||
13
+ absDiff ( t , x ) < absDiff ( ans , x ) ||
14
+ ( absDiff ( t , x ) === absDiff ( ans , x ) && t < ans )
15
+ ) {
16
+ ans = t ;
17
+ }
18
+ }
19
+
20
+ return ans . toString ( ) ;
21
+ }
22
+
23
+ function getCandidates ( n ) {
24
+ const length = n . length ;
25
+ const res = new Set ( ) ;
26
+
27
+ res . add ( BigInt ( Math . pow ( 10 , length - 1 ) - 1 ) ) ;
28
+ res . add ( BigInt ( Math . pow ( 10 , length ) + 1 ) ) ;
29
+
30
+ const left = BigInt ( n . substring ( 0 , Math . ceil ( length / 2 ) ) ) ;
31
+
32
+ for ( let i = left - 1n ; i <= left + 1n ; i ++ ) {
33
+ const prefix = i . toString ( ) ;
34
+ const t =
35
+ prefix +
36
+ prefix
37
+ . split ( '' )
38
+ . reverse ( )
39
+ . slice ( length % 2 )
40
+ . join ( '' ) ;
41
+ res . add ( BigInt ( t ) ) ;
42
+ }
43
+
44
+ res . delete ( BigInt ( n ) ) ;
45
+ return res ;
46
+ }
47
+
48
+ function absDiff ( a , b ) {
49
+ return a > b ? a - b : b - a ;
50
+ }
You can’t perform that action at this time.
0 commit comments