Skip to content

Commit 9218c40

Browse files
authored
feat: add solutions to lc problem: No.0564 (#3445)
1 parent 9cab9d7 commit 9218c40

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

solution/0500-0599/0564.Find the Closest Palindrome/README.md

+55
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,61 @@ func abs(x int) int {
189189
}
190190
```
191191

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+
192247
<!-- tabs:end -->
193248

194249
<!-- solution:end -->

solution/0500-0599/0564.Find the Closest Palindrome/README_EN.md

+55
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,61 @@ func abs(x int) int {
187187
}
188188
```
189189

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+
190245
<!-- tabs:end -->
191246

192247
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}

0 commit comments

Comments
 (0)