Example1: x = 123, return 321
Example2: x = -123, return -321
class Solution {
public:
int reverse(int x) {
bool flag = false;
int result = 0, remainder = 0;
if (x < 0)
{
flag = true;
x = -x;
}
while (x != 0)
{
remainder = x % 10;
result = 10*result + remainder;
x /= 10;
}
if (flag)
{
return -result;
}
return result;
}
};
水题,一水而过
看了下评论才想起有可能溢出,因为测试例子貌似没有给溢出的case,也许出题人也没想到这个问题