Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
class Solution {
public:
int reverse(int x) {
int y = 0;
while(x != 0){
if(y > INT_MAX/10 || y < INT_MIN/10)
return 0;
y = y*10+x%10;
x = x/10;
}
return y;
}
};
本文介绍了一种整数反转算法,并提供了详细的实现代码。该算法能够处理包括负数在内的所有32位整数,并能有效避免反转过程中可能出现的溢出问题。
1520

被折叠的 条评论
为什么被折叠?



