要判断是否会溢出,这点注意一下例如1123456789 反转过来就会超int.
int reverse(int x) { int cur = abs(x);long long ans = 0; int result = 0; while(cur){ result = result*10+cur%10; ans = ans*10+(long long)cur%10; cur/=10; } if(ans!=(long long )result) result = 0; return x<0?0-result:result; }