62,614
社区成员
发帖
与我相关
我的任务
分享
public class Solution {
public int reverse(int x) {
int re = 0;
if (x < 0) {
re = 0 - x;
} else {
re = x;
}
String s = String.valueOf(re);
int length = s.length();
int n = s.length() / 2;
char[] ch = s.toCharArray();
for (int i = 0; i < n; i++) {
char c = ch[i];
ch[i] = ch[length - 1 - i];
ch[length - 1 - i] = c;
}
s = new String(ch);
long a = 0L;
a = Long.parseLong(s);
if (x > 0 && a > Integer.MAX_VALUE) {
return 0;
}
if (x < 0 && a < (0 - Integer.MIN_VALUE)) {
return 0;
}
if (x > 0) {
return Integer.parseInt(s);
} else {
return 0 - Integer.parseInt(s);
}
}
}