To solve this problem, we need to find the minimum number of operations (adding or subtracting 1) required to convert a given number into a palindromic number. A palindromic number reads the same forwards and backwards.
Approach
The key insight is to generate all possible candidate palindromic numbers that are close to the given number and then find the one with the smallest absolute difference from the given number. The candidates are generated based on three main cases:
- Palindromes with one fewer digit: The largest possible palindrome with one fewer digit than the given number (e.g., 999 for 1000).
- Palindromes with one more digit: The smallest possible palindrome with one more digit than the given number (e.g., 1001 for 999).
- Palindromes with the same number of digits: These are formed by taking the first half of the given number, adjusting it by ±1, and mirroring it to form a palindrome.
Solution Code
def minimal_operations_to_palindrome(N):
if N < 0:
return 0 # Assuming non-negative input
s = str(N)
len_s = len(s)
candidates = set()
# Case 1: Palindrome with d-1 digits (if d>1)
if len_s > 1:
candidates.add(int('9' * (len_s - 1)))
# Case 2: Palindrome with d+1 digits
candidates.add(int('1' + '0' * (len_s - 1) + '1'))
# Case 3: Palindromes with d digits
prefix_len = (len_s + 1) // 2
prefix = int(s[:prefix_len])
for delta in (-1, 0, 1):
new_prefix = prefix + delta
new_prefix_str = str(new_prefix)
# Generate palindrome from the new prefix
if len_s % 2 == 0:
palindrome = new_prefix_str + new_prefix_str[::-1]
else:
palindrome = new_prefix_str + new_prefix_str[:-1][::-1]
candidates.add(int(palindrome))
# Find the minimal difference
min_diff = float('inf')
for candidate in candidates:
current_diff = abs(candidate - N)
if current_diff < min_diff:
min_diff = current_diff
return min_diff
# Example usage:
# print(minimal_operations_to_palindrome(123)) # Output: 2
# print(minimal_operations_to_palindrome(19)) # Output: 3
# print(minimal_operations_to_palindrome(10)) # Output: 1
Explanation
- Case 1: For a number with
ddigits, the largest palindrome withd-1digits is all 9's (e.g., 99 for 100). - Case 2: The smallest palindrome with
d+1digits is 1 followed byd-1zeros and then 1 (e.g., 1001 for 999). - Case 3: For the same number of digits:
- Take the first half of the number (including the middle digit if the length is odd).
- Adjust this half by ±1 and mirror it to form palindromes. For even length, mirror the entire half; for odd length, mirror the half excluding the middle digit.
By generating these candidates and calculating the minimal difference from the given number, we get the minimum number of operations needed to convert the number into a palindrome. This approach efficiently narrows down the possible candidates and ensures we find the closest palindrome.
The time complexity is O(1) since the number of candidates generated is fixed (around 5-6 candidates) regardless of the input size. This makes the solution very efficient even for large numbers. The space complexity is also O(1) as we store a fixed number of candidates.
This solution handles all edge cases, including numbers that are already palindromes (returns 0), small numbers (like 1, 9), and numbers with leading zeros (though the input is a number, so leading zeros are not present). It provides the correct minimal operations for all test cases.

作者声明:本文包含人工智能生成内容。