Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Example2: x = -123, return -321
int reverse(int x) {
int sign = 1, res = 0;
if (x<0)sign = -1;
vector< int > v;
x=abs(x);
while(x)
{
v.push_back(x%10);
x = x/10;
}
for(decltype(v.begin()) it = v.begin(); it != v.end(); it++)
{
res = res*10 + (*it);
}
return (res*sign);
}
No comments:
Post a Comment