564. Find the Closest Palindrome Hard
1/**
2 * [564] Find the Closest Palindrome
3 *
4 * Given a string n representing an integer, return the closest integer (not including itself), which is a palindrome. If there is a tie, return the smaller one.
5 * The closest is defined as the absolute difference minimized between two integers.
6 *
7 * Example 1:
8 *
9 * Input: n = "123"
10 * Output: "121"
11 *
12 * Example 2:
13 *
14 * Input: n = "1"
15 * Output: "0"
16 * Explanation: 0 and 2 are the closest palindromes but we return the smallest which is 0.
17 *
18 *
19 * Constraints:
20 *
21 * 1 <= n.length <= 18
22 * n consists of only digits.
23 * n does not have leading zeros.
24 * n is representing an integer in the range [1, 10^18 - 1].
25 *
26 */
27pub struct Solution {}
28
29// problem: https://leetcode.com/problems/find-the-closest-palindrome/
30// discuss: https://leetcode.com/problems/find-the-closest-palindrome/discuss/?currentPage=1&orderBy=most_votes&query=
31
32// submission codes start here
33
34impl Solution {
35 pub fn nearest_palindromic(n: String) -> String {
36 String::new()
37 }
38}
39
40// submission codes end
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45
46 #[test]
47 fn test_564() {
48 }
49}
50
Back
© 2025 bowen.ge All Rights Reserved.