力扣(LeetCode)回文数C++

报错


Line 17: Char 5: error: non-void function does not return a value in all control paths [-Merror lreturn-type]17l}
原因是使用的if语句,只在判断后写了返回,没写else返回

Line 20: Char 36: runtime error: signed integer overflow: 998765432 * 10 cannot be represented in type 'int' (solution.cpp) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:29:36

情景:测试使用了一个特别大的数

我将这个数赋值给了int类型造成报错

更改方式:将int 改成long


1.主要思路是根据回文数特点将数字反转回来,利用对10求余数可以得到这一点

求余数方法:  a%10

2.c++科学计数法的书写方式

2^{31} =2e31

class Solution {
public:
    bool isPalindrome(int x) {

       if(x<0)
       return false;
      else if(x>(2e31-1))
      {
        return false;
      }
      else if(x<-(2e31))
      {
        return false;
      }

        long currentNum=0;
        long startNum =x;
        do
        {
            currentNum =currentNum *10 + x%10;
            x/=10;
        }while(x>0);
        if(currentNum ==startNum)
        return true;
        else 
        return false;

    }
};