Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
class Solution {
public:
int reverse(int x) {
bool flag = x < 0 ?0:1;
ostringstream os;
os<<x;
string str;
str = os.str();
if (!flag) str = str.substr(1);
std::reverse(str.begin(),str.end());
stringstream stream;
stream << str;
int n;
stream >>n;
if (!flag) n = -n;
if (n == 2147483647 || n == -2147483647){
return 0;
}
return (int)n;
}
};