您的位置:首页 > 其它

Leetcode || Reverse Integer

2015-10-23 11:11 393 查看
package pack;
class Solution {
public int reverse(int x) {

if(x > 0) {
return reverse_p(x);
}

else if(x == 0) {
return x;
}

else {
if(x == -2147483648) //如果输入-2147483648,0-x将出错
return 0;
return -reverse_p(0 - x);
}
}

private int reverse_p(int x) {
String s = String.valueOf(x);
StringBuffer sb = new StringBuffer(s);

Long s1 = Long.parseLong(sb.reverse().toString()); //考虑翻转溢出
Long s2 = (long) Integer.MAX_VALUE;

if(s1 > s2)
return 0;

return (int)Integer.parseInt(s1.toString());
}
}

public class Main {
public static void main(String[] args) {
Solution s = new Solution();
//System.out.println(Integer.MAX_VALUE);
System.out.println(s.reverse(-2147483648));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode reverseInt