标签:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
int reverse(int x) { int n; int flag=1; if (x<0) { flag=-1; } string str; stringstream ss,stream; int x1=abs(x); ss<<x1; ss>>str;//将数字X1转换为字符串,存在str中 str=string(str.rbegin(),str.rend());//字符串翻转 if (str.size()>=10&&str>="2147483647")//判断是否溢出 { return 0; } n=atoi(str.c_str());//将字符串转换为数字 return flag*n; }
int reverse(int x) { long long res = 0; while(x) { res = res*10 + x%10; x /= 10; } return (res<INT_MIN || res>INT_MAX) ? 0 : res; }
标签:
原文地址:http://blog.csdn.net/sinat_24520925/article/details/45558769