Solution For Palindrome Number
Problem Statement:
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input: 121
Output: true
Example 2:
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore, it is not a palindrome.
Solution Approach:
The simplest approach to solve this problem is to convert the integer to a string and then check whether the string is a palindrome or not. If the string is a palindrome, then the integer is also a palindrome.
Pseudocode:
Convert the integer to a string
Reverse the string
Compare the original string and the reversed string
If they are equal, return true; otherwise, return false
Solution:
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0) { // Edge case
return false;
}
string s = to_string(x); // Convert the integer to a string
int n = s.length();
for (int i = 0; i < n/2; i++) { // Check if the string is a palindrome
if (s[i] != s[n-1-i]) {
return false;
}
}
return true;
}
};
Time Complexity: O(logn)
Space Complexity: O(logn)
The space complexity is O(logn) because we used an additional variable to store the string representation of the integer.
Step by Step Implementation For Palindrome Number
public class Solution { public boolean isPalindrome(int x) { //negative numbers are not palindromes if (x < 0) { return false; } //initialize reverse number to 0 int reverse = 0; //store original number int original = x; //calculate reverse number while (x > 0) { reverse = reverse * 10 + x % 10; x /= 10; } //compare original number with reverse number return original == reverse; } }
class Solution: def isPalindrome(self, x: int) -> bool: if x < 0: return False if x == 0: return True if x % 10 == 0: return False reverse = 0 while x > reverse: reverse = reverse * 10 + x % 10 x = x // 10 return x == reverse or x == reverse // 10
var isPalindrome = function(x) { // your code here };
class Solution { public: bool isPalindrome(int x) { // Special cases: // When x < 0, x is not a palindrome. // Also if the last digit of the number is 0, in order to be a palindrome, // the first digit of the number also needs to be 0. // Only 0 satisfy this property. if(x < 0 || (x % 10 == 0 && x != 0)) { return false; } int revertedNumber = 0; while(x > revertedNumber) { revertedNumber = revertedNumber * 10 + x % 10; x /= 10; } // When the length is an odd number, we can get rid of the middle digit by revertedNumber/10 // For example when the input is 12321, at the end of the while loop we get x = 12, revertedNumber = 123, // since the middle digit doesn't matter in palidrome(it will always equal to itself), we can simply get rid of it. return x == revertedNumber || x == revertedNumber/10; } };
public bool IsPalindrome(int x) { // Special cases: // As discussed above, when x < 0, x is not a palindrome. // Also if the last digit of the number is 0, in order to be a palindrome, // the first digit of the number also needs to be 0. // Only 0 satisfy this property. if(x < 0 || (x % 10 == 0 && x != 0)) { return false; } int revertedNumber = 0; while(x > revertedNumber) { revertedNumber = revertedNumber * 10 + x % 10; x /= 10; } // When the length is an odd number, we can get rid of the middle digit by revertedNumber/10 // For example when the input is 12321, at the end of the while loop we get x = 12, revertedNumber = 123, // since the middle digit doesn't matter in palidrome(it will always equal to itself), we can simply get rid of it. return x == revertedNumber || x == revertedNumber/10; }