-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_integer.cc
43 lines (43 loc) · 1.13 KB
/
reverse_integer.cc
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
class Solution {
public:
// handle as string
/*
int reverse(int x) {
string in = to_string(x);
string minimal = to_string(INT_MIN).substr(1), maximal = to_string(INT_MAX);
bool negative = false;
if (in[0]=='-') {
negative = true;
in = in.substr(1);
}
//reverse(begin(in), end(in));
//reverse(in.begin(), in.end());
int l = 0, r = in.size() - 1;
while(l<r) swap(in[l++], in[r--]);
if (negative) {
if (in.size()>=minimal.size() && in > minimal) return 0;
return -stoi(in);
} else {
if (in.size()>=maximal.size() && in > maximal) return 0;
return stoi(in);
}
return 0;
}
*/
// handle as number
int reverse(int x) {
long long xx = x, tt = 0;
bool negative = (xx < 0);
xx = llabs(xx);
while (xx) {
tt*=10;
tt += xx%10;
xx /= 10;
}
if (negative) {
tt*=-1;
if (tt < INT_MIN) return 0;
} else if (tt > INT_MAX) return 0;
return tt;
}
};