-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseInteger.cpp
50 lines (46 loc) · 1.1 KB
/
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
class Solution {
public:
int reverse(int x) {
if(x<=INT_MIN || x>INT_MAX || !x)
return 0;
if (x>-10 && x<10)
return x;
int max = 0x7FFFFFFF;
int msb = 0x80000000;
int toggle = 0xFFFFFFFF;
int answer = 0;
bool s = true;
if(x<0) {
// Reversing the two's compliment
x -= 1;
x ^= toggle;
s = false;
}
int i=1;
while(x>0) {
//cout<<"\n "<<i++<<"\t"<<static_cast<long int>(answer)*10;
if (static_cast<long int>(answer)*10 + x%10 > max) {
return 0;
}
answer *= 10;
answer += x%10;
x /= 10;
}
if (!s)
return -answer;
return answer;
}
};
int stringToInteger(string input) {
return stoi(input);
}
int main() {
string line;
while (getline(cin, line)) {
int x = stringToInteger(line);
int ret = Solution().reverse(x);
string out = to_string(ret);
cout << out << endl;
}
return 0;
}