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); int ret = 0; while(x>0){ if((ret > 214748364) || (214748364==ret && x%10 >7)) return 0; ret = ret*10 + x%10; x /= 10; } if(!pos) ret = 0-ret; return ret; }
时间: 2024-10-20 14:47:57