Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
#include <iostream>
using namespace std;
class Solution {
public:
int reverse(int x) {
int result = 0;
int flag = 0;
if (x < 0)
{
x = 0 - x;
flag = 1;
}
while (x)
{
result = result * 10 + x % 10;
x /= 10;
}
if (flag) result = 0 - result;
return result;
}
};
int main()
{
int x;
Solution so;
while (cin >> x) cout << so.reverse(x) << endl;
return 0;
}