问题描述:
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
问题解决:
class Solution {
public:bool isPalindrome(int x) {int sum = 0;int temp = x;if(x < 0){return false;}while(x){sum = sum * 10 + x % 10;x /= 10;}if(temp == sum){return true;}else{return false;}}
};
编译错误:
Line 14: Char 23: runtime error: signed integer overflow: 746384741 * 10 cannot be represented in type 'int' (solution.cpp)
第14行:Char 23: 运行时错误,有符号整型数溢出:746384741 * 10 不能用类型 “int”(solution.cpp)表示
解决方法:
int 溢出,将其改为字节数更大的 double 类型即可。