diff --git a/solution/0500-0599/0564.Find the Closest Palindrome/README.md b/solution/0500-0599/0564.Find the Closest Palindrome/README.md index 123515afac70c..4e94311108f50 100644 --- a/solution/0500-0599/0564.Find the Closest Palindrome/README.md +++ b/solution/0500-0599/0564.Find the Closest Palindrome/README.md @@ -189,6 +189,61 @@ func abs(x int) int { } ``` +#### JavaScript + +```js +/** + * @param {string} n + * @return {string} + */ + +function nearestPalindromic(n) { + const x = BigInt(n); + let ans = null; + + for (const t of getCandidates(n)) { + if ( + ans === null || + absDiff(t, x) < absDiff(ans, x) || + (absDiff(t, x) === absDiff(ans, x) && t < ans) + ) { + ans = t; + } + } + + return ans.toString(); +} + +function getCandidates(n) { + const length = n.length; + const res = new Set(); + + res.add(BigInt(Math.pow(10, length - 1) - 1)); + res.add(BigInt(Math.pow(10, length) + 1)); + + const left = BigInt(n.substring(0, Math.ceil(length / 2))); + + for (let i = left - 1n; i <= left + 1n; i++) { + const prefix = i.toString(); + const t = + prefix + + prefix + .split('') + .reverse() + .slice(length % 2) + .join(''); + res.add(BigInt(t)); + } + + res.delete(BigInt(n)); + return res; +} + +function absDiff(a, b) { + return a > b ? a - b : b - a; +} +``` + diff --git a/solution/0500-0599/0564.Find the Closest Palindrome/README_EN.md b/solution/0500-0599/0564.Find the Closest Palindrome/README_EN.md index 2fdebc09b7c24..2307d363f03e0 100644 --- a/solution/0500-0599/0564.Find the Closest Palindrome/README_EN.md +++ b/solution/0500-0599/0564.Find the Closest Palindrome/README_EN.md @@ -187,6 +187,61 @@ func abs(x int) int { } ``` +#### JavaScript + +```js +/** + * @param {string} n + * @return {string} + */ + +function nearestPalindromic(n) { + const x = BigInt(n); + let ans = null; + + for (const t of getCandidates(n)) { + if ( + ans === null || + absDiff(t, x) < absDiff(ans, x) || + (absDiff(t, x) === absDiff(ans, x) && t < ans) + ) { + ans = t; + } + } + + return ans.toString(); +} + +function getCandidates(n) { + const length = n.length; + const res = new Set(); + + res.add(BigInt(Math.pow(10, length - 1) - 1)); + res.add(BigInt(Math.pow(10, length) + 1)); + + const left = BigInt(n.substring(0, Math.ceil(length / 2))); + + for (let i = left - 1n; i <= left + 1n; i++) { + const prefix = i.toString(); + const t = + prefix + + prefix + .split('') + .reverse() + .slice(length % 2) + .join(''); + res.add(BigInt(t)); + } + + res.delete(BigInt(n)); + return res; +} + +function absDiff(a, b) { + return a > b ? a - b : b - a; +} +``` + diff --git a/solution/0500-0599/0564.Find the Closest Palindrome/Solution.js b/solution/0500-0599/0564.Find the Closest Palindrome/Solution.js new file mode 100644 index 0000000000000..4e17ca3cf65db --- /dev/null +++ b/solution/0500-0599/0564.Find the Closest Palindrome/Solution.js @@ -0,0 +1,50 @@ +/** + * @param {string} n + * @return {string} + */ + +function nearestPalindromic(n) { + const x = BigInt(n); + let ans = null; + + for (const t of getCandidates(n)) { + if ( + ans === null || + absDiff(t, x) < absDiff(ans, x) || + (absDiff(t, x) === absDiff(ans, x) && t < ans) + ) { + ans = t; + } + } + + return ans.toString(); +} + +function getCandidates(n) { + const length = n.length; + const res = new Set(); + + res.add(BigInt(Math.pow(10, length - 1) - 1)); + res.add(BigInt(Math.pow(10, length) + 1)); + + const left = BigInt(n.substring(0, Math.ceil(length / 2))); + + for (let i = left - 1n; i <= left + 1n; i++) { + const prefix = i.toString(); + const t = + prefix + + prefix + .split('') + .reverse() + .slice(length % 2) + .join(''); + res.add(BigInt(t)); + } + + res.delete(BigInt(n)); + return res; +} + +function absDiff(a, b) { + return a > b ? a - b : b - a; +}