*#9.Palindrome Number*

### Problem statement

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 while`123`

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

### Explanation

็ตฆๅฎไธๅ `x`

็ๆดๆธ๏ผๅฆๆๆฏๅๆๅ่ฟๅ `true`

๏ผๅฆๅ `false`

ๅๆ็ๅฎ็พฉๆฏไธ่ซๅพ้ ญๆๅพๅฐพ้ๅง๏ผ่ฎ่ตทไพๆฏไธๆจฃ็๏ผไพๅฆ `121`

ๅ `1221`

ๆฏๅๆ๏ผไฝ `123`

ไธๆฏ

### Solution

้้ก่ฆ่ๆ ฎ็ๅฐๆนๆฏๅๆ็้ทๅบฆ๏ผๅถๆธ้ทๅบฆ็ๅๆๅๅฒ็บๅทฆๅณๅ ฉ็ญๅๆๆฏ่ผๅฅฝๅคๆท๏ผๅฅๆธ้ทๅบฆ็ๅๆๆๅพๆ็ไธไธญ้็ๆธ๏ผๅฎไธ้่ๅ ถไปๆธๅฐๆ๏ผไธ้ก็ฎๆๅพ็ Follow up ้ๆๅฐ่ฝๅฆๅจไธๅฐๆธๅญ่ฝๆๆๅญไธฒ็ๆ ๆณไธไพ่งฃ๏ผไพ็ งๅ่ฟฐ็ๅๆๅ้กๆ๏ผๆญค้กๅฏไฝฟ็จ็ฎ่ก้็ฎๅญ๏ผArithmetic operators๏ผไพ่งฃใ

ไธ้ๅงๅ ้ๆฟพไธไบ็นๅฎ็ๆขไปถ

- ๅคๆท
`x`

ๆฏๅฆ็บ 0๏ผๅฆๆๆฏ return true - ๅคๆท
`x`

ๆฏๅฆๅฐๆผ 0 ๆ่ ็บ 10 ็ๅๆธ๏ผๅฆๆๆฏ return false

ๆฅ่ๅท่ก `while`

่ฟดๅ็ดๅฐๆขไปถๆ็ซ๏ผๆๅพ return `x`

่ๆทๅๅบ็ `reverseNum`

ๆฏๅฆ็ธ็ญใ

```
public bool IsPalindrome(int x)
{
if (x == 0)
return true;
if (x < 0 || x % 10 == 0)
return false;
int reverseNum = 0;
while (x > reverseNum)
{
reverseNum = reverseNum * 10 + x % 10;
x /= 10;
}
return x == reverseNum || x == reverseNum / 10;
}
```

### Reference

Thanks for reading the article ๐ท ๐ป ๐ผ

If you like it, please don't hesitate to click heart button โค๏ธ

or click like on my Leetcode solution

or follow my GitHub โญ

or buy me a coffee โฌ๏ธ I'd appreciate it.

## Oldest comments (0)