Practice/Meta/Leetcode 564. Find the Closest Palindrome
CodingMust
You are given a string n representing a positive integer. Your task is to find the closest palindrome number to the given number, excluding the number itself.
A palindrome number reads the same forwards and backwards. If there are two palindromes at the same distance from the given number, return the smaller one.
The input string can represent very large numbers (up to 18 digits).
1 <= n.length <= 18n consists of only digitsn does not have leading zerosn represents an integer in the range [1, 10^18 - 1]Example 1:
Input: n = "123" Output: "121" Explanation: The closest palindrome to 123 is 121 (difference of 2).
Example 2:
Input: n = "1" Output: "0" Explanation: The closest palindrome to 1 is 0. Note that 2 is also distance 1 away, but we return the smaller value.
Example 3:
Input: n = "99" Output: "101" Explanation: 101 is distance 2 from 99, while 88 is distance 11, so 101 is closer.
Example 4:
Input: n = "1000" Output: "999" Explanation: Both 999 and 1001 are distance 1 from 1000, but we return the smaller value 999.