Algorithm
Problem - Palindrome Number
URL - https://leetcode.com/problems/palindrome-number/
Level - Beginner
Details -
Given an integer x
, return true
if x
is palindrome integer.
An integer is a palindrome when it reads the same backward as forward.
- For example,
121
is a palindrome while123
is not.
Example 1:
Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left.
Example 2:
Input: x = -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: x = 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Constraints:
-231 <= x <= 231 - 1
Code Examples
#1 Code Example with Javascript Programming
Code -
Javascript Programming
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
/**
* Solution 1 : JavaScript way
*/
// const reverseNumber = x.toString().split("").reverse().join("");
// return x == reverseNumber;
/**
* Solution 2: Traditional Programming way
*/
let initialX = x,
reverseNumber = 0;
// Reverse the number.
while (x != 0) {
reverseNumber = parseInt(reverseNumber * 10 + parseInt(x % 10));
x = parseInt(x / 10);
}
// Handle for negative numbers.
if (initialX < 0) {
return initialX === -1 * reverseNumber;
}
return initialX === reverseNumber;
};
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C Programming
Code -
C Programming
bool isPalindrome(int x) {
int n, d, k = 0;
if (x < 0) return false;
n = x;
while (n) {
d = n % 10;
if (k > (0x7fffffff - d) / 10) return false; // overflow
k = k * 10 + d;
n = n / 10;
}
return (k == x);
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with C++ Programming
Code -
C++ Programming
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0 || (x != 0 && x%10 == 0)) return false;
int y = 0;
while (x > y){
y = y*10 + x%10;
x = x/10;
}
return (x==y || x==y/10);
}
};
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with C# Programming
Code -
C# Programming
namespace LeetCode
{
public class _009_PalindromeNumber
{
public bool IsPalindrome(int x)
{
if (x < 0) { return false; }
if (x < 10) { return true; }
var temp = x;
var y = 0;
var digit = 0;
while (temp != 0)
{
digit = temp % 10;
y = y * 10 + digit;
temp /= 10;
}
return x == y;
}
}
}
Copy The Code &
Try With Live Editor
Input
Output
#5 Code Example with Java Programming
Code -
Java Programming
class Solution {
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
}
int reversedNum = 0;
int xCopy = x;
while (x > 0) {
int rem = x % 10;
reversedNum = reversedNum * 10 + rem;
x /= 10;
}
return reversedNum == xCopy;
}
}
Copy The Code &
Try With Live Editor
Input
Output
#6 Code Example with Python Programming
Code -
Python Programming
class Solution:
def isPalindrome(self, x):
return str(x) == str(x)[::-1]
Copy The Code &
Try With Live Editor
Input
Output
Demonstration
Leetcode - Palindrome Number Problem solution in Javascript Leetcode