leecode第九题(回文数)

class Solution {
public:
    bool isPalindrome(int x) {
        if (x<0)
            return false;
        
        long y=0;//这里使用long,也不判断溢出了,反正翻转不等就不是回文
        int a=x;
        while (a!=0)
        {
            y=(a%10)+y*10;
            a=a/10;
        }

        if (y==x)
            return true;
        else
            return false;
    }
};

 分析:

我看到人家问能不能不用字符串,第一反应是,哦,可以用字符串啊。。。。

这个思路好像也不难,就不写字符串了,但是我还是出现了两处失误,一处是写的太快了,脑子没跟上手,一个就是那个long,之前int,碰到一个溢出的数,按理是要考虑的。

原文地址:https://www.cnblogs.com/CJT-blog/p/10576786.html