题目
思路
主要是处理整数反转后溢出的问题,用long long类型的变量m存储反转的整数,若溢出m=0,返回m。
代码
class Solution {
public:
int reverse(int x) {
const int int_max=pow(2,31)-1;
const int int_min=-pow(2,31);
long long m=0;
while(x!=0)
{
m=m*10+(x%10);
x/=10;
}
if(m<int_min||m>int_max)
{
m=0;
}
return m;
}
};
结果
注意:c++计算幂,需添加头文件cmath,pow(),powl(),powf()。