Appearance

# Palindrome Number Easy

## Question

Given an integer x, return true if x is a palindrome, and false otherwise.

txt

```
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.
```

txt

```
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.
```

txt

```
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
```

txt

`-231 <= x <= 231 - 1`

## Solution

python

```
def isPalindrome(x):
x_str = str(x)
return x_str == x_str[::-1]
```

java

```
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
}
int original = x;
int reversed = 0;
while (x != 0) {
int pop = x % 10;
x /= 10;
// Check for integer overflow
if (reversed > (Integer.MAX_VALUE - pop) / 10) {
return false;
}
reversed = reversed * 10 + pop;
}
return original == reversed;
}
```

javascript

```
function isPalindrome(x) {
if (x < 0) {
return false;
}
const original = x;
let reversed = 0;
while (x > 0) {
const digit = x % 10;
reversed = reversed * 10 + digit;
x = Math.floor(x / 10);
}
return original === reversed;
}
```

## Notes

You are given an integer `x`

. You need to determine if `x`

is a palindrome, which means it reads the same forwards and backwards.

### Time and Space Complexity Analysis

The time complexity of the given solutions is `O(log(x))`

, where `x`

is the given integer. The number of digits in `x`

determines the number of iterations required to reverse the integer.

The space complexity for all solutions is `O(1)`

because they only use a few extra variables to store intermediate results.

These solutions handle checking for palindromes efficiently while also considering integer overflow, especially in the Java and JavaScript implementations.