-
Notifications
You must be signed in to change notification settings - Fork 0
/
7-ReverseInteger.cpp
101 lines (76 loc) · 1.95 KB
/
7-ReverseInteger.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Update (2014-11-10):
Test cases had been added to test the overflow behavior.
*/
#include <stdio.h>
#include <vector>
using namespace std;
class Solution {
public:
bool muti_ok(int x, int y) {
long long pll = (long long) x * y;
return pll == (int) pll;
}
bool add_ok(int x, int y) {
int sum = x + y;
int neg_over = x < 0 && y < 0 && sum >= 0;
int pos_over = x >= 0 && y >= 0 && sum < 0;
return !neg_over && !pos_over;
}
int GetPower(unsigned x) {
int result = 1;
if (x == 0) {
return 1;
}
for (unsigned i = 1; i <= x; i++) {
if (!muti_ok(result, 10)) {
return 0;
}
result *= 10;
}
return result;
}
int reverse(int x) {
int result = 0;
vector<int> store;
while (x) {
store.push_back(x % 10);
x /= 10;
}
size_t size = store.size();
for (size_t i = 0; i < store.size(); i++) {
int pre = result;
if (GetPower(size - i - 1) == 0) {
return 0;
}
int power = GetPower(size - i - 1);
if (!muti_ok(power, store[i])) {
return 0;
}
int step = store[i] * power;
if (!add_ok(result, step)) {
return 0;
}
result += step;
// Check OverFlow
}
return result;
}
};
int main() {
int source = 10;
printf("source: %d\n", source);
Solution s;
int result = s.reverse(source);
printf("result: %d\n", result);
return 0;
}