> For the complete documentation index, see [llms.txt](https://cai-sen-se.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cai-sen-se.gitbook.io/leetcode/1-200/9.-hui-wen-shu.md).

# 9. 回文数

<https://leetcode-cn.com/problems/palindrome-number/>

## 解法一：

从原数x最低位开始切，切出的数为y。将x与y比较看是否相等。&#x20;

特殊情况：负数肯定不是回文数（因为有负号），末尾为0的数，除了0以外都不是回文（因为除0以外最高位不可能有0）。

```cpp
class Solution {
public:
    bool isPalindrome(int x) {
        if (x < 0 || x % 10 == 0 && x != 0) return false;
        int reverse = 0;
        while (x > reverse) {    //到x<=reverse为止
            reverse = reverse * 10 + x % 10;
            x /= 10;
        }
        //第一种情况，原数为偶数位；第二种，原数为奇数位，while退出时会有reverse = x
        return (x == reverse || x == reverse / 10);
    }
};
```

## 解法二：py字符处理

题目不让用字符，然而py怎能不字符

```python
class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x < 0:
            return False
        return  x == int(str(x)[::-1])
```
