๐ Problem Description
Please enter an integer x and determine whether it is a palindrome (i.e., it reads the same forwards and backwards).
Requirements๏ผ
-
Do not use string conversion
-
Handle negative numbers (e.g., -121 is not a palindrome)
-
Advanced: Avoid reversing the entire number to optimize time complexity
Example:
Input: 121 โ Output: is a palindrome Input: -121 โ Output: is not a palindrome Input: 12321 โ Output: is a palindrome
Difficulty:โญโญ
๐ก Basic Implementation (Reverse the entire number)
#include <stdio.h>#include <stdbool.h>
bool isPalindrome(int x) { if (x < 0) return false; // Negative numbers return false directly long reversed = 0; // Prevent overflow during reversal int original = x;
while (x > 0) { reversed = reversed * 10 + x % 10; x /= 10; } return reversed == original;}
int main() { int x; printf("Input integer:"); scanf("%d", &x); printf("%d %s palindrome", x, isPalindrome(x) ? "is" : "is not"); return 0;}
๐ฅ Advanced Optimization (Reverse half the number)
Avoid reversing the entire number to prevent overflow:
bool isPalindrome_opt(int x) { if (x < 0 || (x % 10 == 0 && x != 0)) return false; // Handle negative numbers and cases ending with 0 int reversedHalf = 0; // Stop the loop when the original number <= reversed half while (x > reversedHalf) { reversedHalf = reversedHalf * 10 + x % 10; x /= 10; } // Even length: x == reversedHalf // Odd length: x == reversedHalf / 10 return x == reversedHalf || x == reversedHalf / 10;}
Key Points๏ผ
-
Time Complexity O(logโโn): Only reverse half the number
-
Space Complexity O(1): No extra memory required
๐ฅ Extended Features
1. Comparison of String Methods
#include <string.h>bool isPalindrome_str(int x) { char str[20]; sprintf(str, "%d", x); int len = strlen(str); for (int i = 0; i < len/2; i++) { if (str[i] != str[len-1-i]) return false; } return true;}
2. Handling Large Numbers (long long type)
bool isPalindrome_ll(long long x) { if (x < 0) return false; long long reversed = 0, original = x; while (x > 0) { reversed = reversed * 10 + x % 10; x /= 10; } return reversed == original;}
๐ค Common Problem Analysis
| Problem | Error Cause | Solution |
|---|---|---|
| Overflow not handled | Reversal exceeds int range | Use<span>long</span> type to store the reversed value |
| Misjudgment of numbers ending with 0 | e.g., x=10 returns true | Add<span>x%10 ==0 && x!=0</span> check |
| Incorrect judgment of odd-length numbers | Middle digit not handled | Allow<span>reversedHalf/10</span> when reversing half |
๐ Upcoming Preview
No. 24: Finding Duplicate Numbers in an Array
๐ฏ Extended Challenge
Try to implement the following features (choose any one)๏ผ
-
Recursive implementation of palindrome checking
-
Count all palindromes within a range (e.g., 1~10000)
-
Check palindrome strings (e.g., “abcba”)
๐ข Interaction TimeWhat pitfalls have you encountered when checking for palindromes? Feel free to leave a comment!
๐If you find this useful, feel free to share it with friends learningC language!
Author of this article:Vv Computer Graduate Examination World (focusing on computer graduate examination tutoring for 8 years)
Original Statement: Please contact for authorization to reprint, infringement will be pursued.