# 11. 回文数判断

java

```java
package leetcode.all.solution1_100;

/**
 * 回文数.
 * 判断一个整数是否是回文数。回文数是指正序（从左向右）和倒序（从右向左）读都是一样的整数。
 *
 * 示例 1:
 * 输入: 121
 * 输出: true
 *
 * 示例 2:
 * 输入: -121
 * 输出: false
 * 解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
 *
 * 示例 3:
 * 输入: 10
 * 输出: false
 * 解释: 从右向左读, 为 01 。因此它不是一个回文数。
 *
 * 进阶:
 *
 * 你能不将整数转为字符串来解决这个问题吗？
 *
 */
public class Solution9 {

    //策略1，反转所有的数字，比较和原值是否相同.
    public boolean isPalindrome(int x) {
        int t=x;
        if(x<0) return false;
        if(x<10) return true;
        long m=0;
        while(x!=0){
            m=(m+x%10)*10;
            x=x/10;
        }

        m=m/10;
        if(m==t) return true;
        return false;
    }

    //策略2，反转一半的数字.
    public boolean isPalindrome2(int x) {
        // 特殊情况：
        // 如上所述，当 x < 0 时，x 不是回文数。
        // 同样地，如果数字的最后一位是 0，为了使该数字为回文，
        // 则其第一位数字也应该是 0
        // 只有 0 满足这一属性
        if(x<0||(x%10==0&&x!=0)) return false;

        int revertedNumber=0;
        while(x>revertedNumber){
            revertedNumber=revertedNumber*10+x%10;
            x/=10;
        }

        // 当数字长度为奇数时，我们可以通过 revertedNumber/10 去除处于中位的数字。
        // 例如，当输入为 12321 时，在 while 循环的末尾我们可以得到 x = 12，revertedNumber = 123，
        // 由于处于中位的数字不影响回文（它总是与自己相等），所以我们可以简单地将其去除。
        return x==revertedNumber||x==revertedNumber/10;
    }

    public static void main(String[] args) {
        int x=1000000001;
        boolean b = new Solution9().isPalindrome(x);
        boolean b2 = new Solution9().isPalindrome2(x);
        System.out.println("Method1:"+b);
        System.out.println("Method2:"+b2);
    }
}
```

python

```python
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/zhi-shang-lei/11-hui-wen-shu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
