Description:
Given an integer x
, return true
if x
is a palindrome, and false
otherwise.
Examples:
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.
Solution in Python:
Python
class Solution:
def isPalindrome(self, x: int) -> bool:
# If x is negative, it cannot be a palindrome
if x < 0:
return False
# Convert the integer to a string to easily check for palindrome properties
str_x = str(x)
# Compare the string with its reverse
return str_x == str_x[::-1]
Explanation:
- Check for Negative Numbers:
- If the integer
x
is negative, it cannot be a palindrome because a negative sign would only be on the left side. Therefore, the function immediately returnsFalse
for negative numbers.
- If the integer
- Convert Integer to String:
- Convert the integer
x
to its string representation usingstr(x)
. This makes it easier to compare the number with its reverse.
- Convert the integer
- Compare with Reverse:
- The string is compared with its reverse. In Python,
str_x[::-1]
gives the reverse of the stringstr_x
. - If the original string is the same as its reverse, the number is a palindrome, so the function returns
True
. Otherwise, it returnsFalse
.
- The string is compared with its reverse. In Python,
Solution in Javascript:
JavaScript
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
// If x is negative, it cannot be a palindrome
if (x < 0) {
return false;
}
// Convert the integer to a string to easily check for palindrome properties
let strX = x.toString();
// Compare the string with its reverse
let reversedStrX = strX.split('').reverse().join('');
return strX === reversedStrX;
};
Solution in Java:
Java
class Solution {
public boolean isPalindrome(int x) {
// If x is negative, it cannot be a palindrome
if (x < 0) {
return false;
}
// Convert the integer to a string to easily check for palindrome properties
String strX = Integer.toString(x);
// Initialize pointers for the start and end of the string
int left = 0;
int right = strX.length() - 1;
// Compare characters from both ends towards the center
while (left < right) {
// If characters at the current pointers do not match, it's not a palindrome
if (strX.charAt(left) != strX.charAt(right)) {
return false;
}
// Move the pointers towards the center
left++;
right--;
}
// If all characters matched, it is a palindrome
return true;
}
}
Solution in C#:
C#
public class Solution {
public bool IsPalindrome(int x) {
// If x is negative, it cannot be a palindrome
if (x < 0) {
return false;
}
// Convert the integer to a string to easily check for palindrome properties
string strX = x.ToString();
// Initialize pointers for the start and end of the string
int left = 0;
int right = strX.Length - 1;
// Compare characters from both ends towards the center
while (left < right) {
// If characters at the current pointers do not match, it's not a palindrome
if (strX[left] != strX[right]) {
return false;
}
// Move the pointers towards the center
left++;
right--;
}
// If all characters matched, it is a palindrome
return true;
}
}