blob: 14f55cdc9636c6227902340bba8fd4e0f01d322f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
// 7. Reverse Integer
#include "leetcode.h"
/*
* given a signed 32-bit integer 'x', return 'x' with its digits reversed. if
* reversing 'x' causes the value to go outside the signed 32-bit integer range,
* then return 0
*/
class Solution {
public:
int reverse(int x) {
int ans = 0;
while (x) {
int tmp = ans * 10 + x % 10;
if (tmp / 10 != ans)
return 0;
ans = tmp;
x /= 10;
}
return ans;
}
};
int main() {
Solution obj;
printf("%d --> %d\n", 123, obj.reverse(123));
printf("%d --> %d\n", -123, obj.reverse(-123));
printf("%d --> %d\n", 120, obj.reverse(120));
}
|