-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7-reverse-integer.c
More file actions
61 lines (48 loc) · 1.3 KB
/
Copy path7-reverse-integer.c
File metadata and controls
61 lines (48 loc) · 1.3 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <string>
#include <cassert>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
/*
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 [-2^31, 2^31 - 1], then return 0.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
Example 1:
Input: x = 123
Output: 321
Example 2:
Input: x = -123
Output: -321
Example 3:
Input: x = 120
Output: 21
Constraints:
-231 <= x <= 231 - 1
*/
class Solution {
public:
int reverse(int x) {
int result = 0;
int ex = abs(x);
while(ex > 0) {
long int part = (long int)result * 10 + (ex % 10);
if(part > numeric_limits<int>::max())
return 0;
result = part;
ex /= 10;
}
return (x<0)? -result : result;
}
};
int main(int argc, char * argv[]) {
Solution sol;
assert(sol.reverse(123) == 321);
assert(sol.reverse(-123) == -321);
assert(sol.reverse(120) == 21);
assert(sol.reverse(1534236469) == 0);
assert(sol.reverse(-2147483648) == 0);
cout << "tests is passed!" << endl;
return 0;
}