public class Solution {
public int reverse(int x) {
boolean negative = x < 0;
x = Math.abs(x);
long ret = 0;
while (x > 0) {
ret = ret * 10 + x % 10;
if (ret != (int)ret) return 0;
x /= 10;
}
return negative ? -(int)ret : (int)ret;
}
}typedef long long ll;
class Solution {
public:
int reverse(int x) {
int sign = x > 0 ? 1 : -1;
ll n = abs((ll)x);
ll res = 0;
while (n > 0) {
res = res * 10 + n % 10;
n /= 10;
}
res *= sign;
// 0x80000000 会被认为是 ul 类型,所以要转换为int才是 Integer.MIN_VALUE
if (res < int(0x80000000) || res > 0x7fffffff) {
return 0;
} else {
return (int)res;
}
}
};