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;
}
}
当中要明白,如果,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;
}
}
相关文章推荐
- 最终用户计算一周观察 001
- Win7 Python2.7 安装教程
- matlab中lsim的用法
- MATLAB Lsim
- 从零开始开发JVM语言(七)语义分析的起步
- 反转单链表的几种方法
- uva1608 non-boring sequences
- 呆在饥饿边缘
- CentOS7+JDK1.7
- 数组操作
- [javaSE] IO流(RandomAccessFile)
- C/C++输出格式运用
- 显式锁ReentrantLock
- <<金刚经>> 读书小记
- Ubuntu14.04桥接网络设置与SSH登陆
- Hibernate-缓存
- c/C++ 田忌赛马(贪心算法)
- Mysql Date 函数
- 每日Scrum(7)
- 异常_File