java.lang.Object
g0501_0600.s0564_find_the_closest_palindrome.Solution

public class Solution extends Object
564 - Find the Closest Palindrome.<p>Hard</p> <p>Given a string <code>n</code> representing an integer, return <em>the closest integer (not including itself), which is a palindrome</em>. If there is a tie, return <em><strong>the smaller one</strong></em>.</p> <p>The closest is defined as the absolute difference minimized between two integers.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = &ldquo;123&rdquo;</p> <p><strong>Output:</strong> &ldquo;121&rdquo;</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = &ldquo;1&rdquo;</p> <p><strong>Output:</strong> &ldquo;0&rdquo;</p> <p><strong>Explanation:</strong> 0 and 2 are the closest palindromes but we return the smallest which is 0.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n.length <= 18</code></li> <li><code>n</code> consists of only digits.</li> <li><code>n</code> does not have leading zeros.</li> <li><code>n</code> is representing an integer in the range <code>[1, 10<sup>18</sup> - 1]</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • nearestPalindromic

      public String nearestPalindromic(String n)