Reverse Integer
C
1#include<string>
2#include<stdio.h>
3#include<stdlib.h>
4using namespace std;
5class Solution {
6public:
7 int reverse(int x) {
8 if(x == 0 || x < -2147483647 || x > 2147483647){
9 return 0;
10 }
11 bool negative = false;
12 if(x < 0){
13 negative = true;
14 x = x * -1;
15 }
16 string ans = "";
17 while(x != 0){
18 ans += std::to_string(x%10);
19 x /= 10;
20 }
21 try{
22 int res = std::stoi(ans);
23 }
24 catch(std::out_of_range& e){
25 return 0;
26 }
27 if(negative)
28 return std::stoi(ans) * -1;
29 return std::stoi(ans);
30
31 }
32};