LeetCode 9: Palindrome Number

题目描述:

Determine whether an integer is a palindrome. Do this without extra space.

思路:

对数字不停对10取余,并将余数乘10累加,比较剩下的数字与余数的大小,直至相等或者数字对10取余后相等,返回true,否则返回false

几种特殊情况:

1.负数不是回文数

2.末尾为0的数不是回文数

3.0是回文数

    bool isPalindrome(int x){
        if(x==0) return true;
        if(x<0||x%10==0) return false;

        int x2 = 0;
        while(x/10>x2){
            x2 = x2*10 +x%10;
            x = x/10;
        }
        if(x/10==x2||x==x2){
            return true;
        }
        return false;
    }
原文地址:https://www.cnblogs.com/xiamaogeng/p/4350789.html