-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7ReverseInteger.cpp
More file actions
53 lines (39 loc) · 863 Bytes
/
Copy path7ReverseInteger.cpp
File metadata and controls
53 lines (39 loc) · 863 Bytes
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
#include <iostream>
#include <cmath>
#include <climits>
using namespace std;
class Solution {
public:
int reverse(int x) {
const int MAX = 10;
int nums[MAX];
for (int i = 0;i < MAX;i++)
nums[i] = 0;
bool flag = x >= 0 ? true : false;
int mx = x >= 0 ? x : -x;
int k = 0;
while (mx)
{
nums[k++] = mx%10;
mx /= 10;
}
long long t = 0;
for (int i = 0; i < k; i++)
{
t = 10 * t + nums[i];
}
if (t > INT_MAX || t < INT_MIN)
return 0;
else
return flag ? t : -t;
}
};
int main()
{
Solution m;
cout << m.reverse(123) << endl;
cout << m.reverse(-123) << endl;
cout << m.reverse(100) << endl;
cout << m.reverse(10) << endl;
return 0;
}