-
Notifications
You must be signed in to change notification settings - Fork 1
/
7.ReverseInteger.cpp
66 lines (65 loc) · 1.36 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
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
int ireverse(int x) {
int flag = false;
std::string cs = "2147483647";
if(x < 0){
flag = true;
x = -x;
if(x == -2147483648)
return 0;
}
std::stringstream ss;
std::string sn;
ss << x;
ss >> sn;
reverse(sn.begin(),sn.end());
if(sn > cs && sn.size() == cs.size()) return 0;
int n;
ss.clear();
ss << sn;
ss >> n;
if(flag) n = -n;
return n;
}
class Solution {
public:
int reverse(int x) {
long long lx = x;
if(lx > ~(1 << 31) || lx < (1 << 31))
return 0;
bool isF = false;
if(lx < 0){
lx = -lx;
isF = true;
}
long long res = 0;
while(lx){
long long c = lx%10;
lx/=10;
res = res*10 + c;
if(res > 2147483647)
return 0;
}
return isF?-res:res;
}
};
int main()
{
// std::cout << sizeof(int) << std::endl;
// int x = 0;
// int px = -1;
// for(int i = 0;i >= 0;i++){
// x++;
// px++;
// }
// std::cout << x << " " << px;
long int test = 0;
int a = 1 << 31;
int b = a^(~0);
std::cout << b << std::endl;
std::cout << a << std::endl;
std::cout << ir(123);
}