文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description

2. Solution
- Two loops
1 | class Solution { |
- One loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20class Solution {
public:
int reverse(int x) {
if(x == 0 || x == INT_MIN) {
return 0;
}
int y = abs(x);
int result = 0;
int remainder = 0;
while(y) {
remainder = y % 10;
if((INT_MAX - remainder) / 10 < result) {
return 0;
}
result = result * 10 + remainder;
y /= 10;
}
return x == abs(x)?result:-result;
}
};