题目:回文数

看到这道题我第一个想到的就是之前的翻转整数的题,然后根据题目条件我决定先把负数跟一位数的返回,然后把剩下情况翻转整数,最后比较翻转的结果跟原来的结果,相同返回true。

static const auto io_speed_up = []()
{
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);
    return 0;
}();

class Solution {
public:
    bool isPalindrome(int x) {
        if (x < 0)
            return false;
        if (x < 10)
            return true;
        int res  = 0;
        for(int temp = x; temp > 0; temp /= 10)
            res = res * 10 + temp % 10;

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

猜你喜欢

转载自www.cnblogs.com/change4587/p/9172999.html