LeetCode刷题:9.回文数+注意事项

前言

  • 文章作为日记或心得,记录学习过程
  • 本文记录本题(源自LeetCode)遇到的所有问题、疑惑
  • 如对内容有任何建议或看法,欢迎评论区学习交流

正文

题目

在这里插入图片描述

解答

该题和上回的整数反转相似,无非最后多了个判断

class Solution {
public:
    bool isPalindrome(int x) {
        int tran=0;
        if(x<0)
            return false;
        else if(x==0)
            return true;
        else
        {
            int x1=x;
            while(x1>0)//这里写成while(x1)也可
            {
                if(tran>INT_MAX/10||tran<INT_MIN/10)//这里仍需注意tran的取值范围,防止溢出
                    return false;
                tran=tran*10+x1%10;
                x1=x1/10;
            }
            if(tran==x)
                return true;
            else
                return false;
        }
    }
};

结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/wwz1751879/article/details/107837577