您的位置:首页 > 其它

Reverse Integer

2016-06-07 21:27 190 查看
依据加强的测试用例,很多参考已经不能通过。

当中要明白,如果,x是Integer.MIN_VALUE,Math.abs()仍只返回Integer.MIN_VALUE

public class Solution {
public int reverse(int x) {

if (x > -10 && x < 10) {
return x;
}
if (x == Integer.MAX_VALUE) {
return 0;
}
if (x == Integer.MIN_VALUE) {
return 0;
}
boolean positive = true;
int num = x;
if (x < 0) {
positive = false;
//num = Math.abs(x);
//num = -x;
num = 0-x;
}
String strN = String.valueOf(num);
StringBuilder sb = new StringBuilder(strN);
strN = sb.reverse().toString();
double dN = Double.valueOf(strN);
if (!positive) {
dN = -dN;
}
if (dN > Integer.MAX_VALUE) {
return 0;
}
if (dN < Integer.MIN_VALUE) {
return 0;
}
return (int) dN;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: