Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
class Solution {
public:
int reverse(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int flag = x>0? 1:-1;
int X = abs(x);
int result = 0;
while (X)
{
result = result*10 + X%10;
X /= 10;
}
return flag * result;
}
};