LeetCode 7: Reverse integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321.

代码

1
2
3
4
5
6
7
8
9
10
11
int reverse(int x) {
int res = 0;
while (x != 0) {
if (res > INT_MAX/10 ||res < INT_MIN/10) {
return 0;
}
res = x % 10 + res * 10;
x /= 10;
}
return res;
}
分享到 评论