7. Reverse Integer (整数的溢出)
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
int reverse(int x) { if(x == INT_MIN){//-2147483648 return 0; } bool pos = x>= 0? true:false; x = abs(x); long int ret = 0; while(x>0){ ret = ret*10 + x%10; if(ret > INT_MAX ) return 0; x /= 10; } if(!pos) ret = 0-ret; return ret; }