-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
7.cpp
110 lines (105 loc) · 2.8 KB
/
7.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
102
103
104
105
106
107
108
109
110
__________________________________________________________________________________________________
4ms
class Solution {
public:
int reverse(int x) {
if(x >= INT_MAX || x <= INT_MIN){
return 0;
}
int temp = x>=0 ? x: (0-x);
long sum =0;
while(temp/10 !=0){
sum = sum*10 + temp%10;
temp = temp/10;
}
sum = sum*10 + temp;
if(sum >= INT_MAX){
return 0;
}
return x>0 ? sum : 0-sum;
}
};
__________________________________________________________________________________________________
4ms
class Solution {
public:
int reverse(int x) {
int res = 0;
while (x != 0) {
if (abs(res) > INT_MAX / 10) return 0;
res = res * 10 + x % 10;
x /= 10;
}
return res;
}
};
__________________________________________________________________________________________________
16ms
class Solution {
public:
int reverse(int x) {
stringstream ss;
ss << x;
string s;
ss >> s;
std::reverse(s.begin(),s.end());
if (s[s.length() - 1] == '-')
s= '-' + s.substr(0, s.length() - 1);
ss.clear();
//stringstream ss2;
ss << s;
ss >> x;
if (x == 0x7fffffff || x == 0x80000000)
x = 0;
return x;
}
};
__________________________________________________________________________________________________
8096 kb
class Solution {
public:
int reverse(int x) {
std::ios::sync_with_stdio(NULL);
std::cin.tie(NULL);
std::cout.tie(NULL);
long int revNum = 0;
bool isNeg = x < 0;
while(x != 0)
{
revNum += x % 10;
x /= 10;
if(x > 0 || isNeg)
revNum *= 10;
}
if(isNeg)
{
revNum /= 10;
}
return revNum >= INT_MAX || revNum <= INT_MIN ? 0 : int(revNum);
}
};
__________________________________________________________________________________________________
8100 kb
class Solution {
public:
int reverse(int x) {
int res = 0;
std::ios::sync_with_stdio(NULL);
std::cin.tie(NULL);
std::cout.tie(NULL);
constexpr int max = INT_MAX / 10;
constexpr int min = INT_MIN / 10;
int r = 0;
while (x) {
if ((max < res) || ((max == res) && x > 7) ||
(res < min) || ((res == min) && x < - 8)) {
res = 0; x = 0; //*/return 0;
}
r = x % 10;
res = r + 10 * res;
x /= 10;
}
return res;
}
};
__________________________________________________________________________________________________