其他分享
首页 > 其他分享> > 【LeetCode 9】回文数

【LeetCode 9】回文数

作者:互联网

题目链接

【题解】


还是要注意,取反的时候,-2^31 取反的话会爆掉Int。。(因为int的正数最多到2^31-1)

【代码】

class Solution {
    public:           
    bool isPalindrome(int x) {
        int f = -1;
        string s;
        s = "";
        if (x<0) {
            s+="-";
            f = 1;
        }
        while (f*x<0){
            char key = (x%10)+'0';
            s = key+s;
            x/=10;
        }
        string ts = s;
        reverse(ts.begin(),ts.end());
        if (ts==s)
            return true;
        else return false;
    }

};

标签:int,题解,31,取反,bool,爆掉,LeetCode,回文
来源: https://www.cnblogs.com/AWCXV/p/11790546.html