leetcode 整数反转

1032 / 1032 个通过测试用例
状态:通过
执行用时:4 ms
内存消耗:33.7 MB

class Solution {
    public int reverse(int x) {
        if (x == 0) return 0;
        boolean is = false;
        long lon = x;
        if (lon < 0) {lon = Math.abs(lon);is = true;}
        StringBuilder sb = new StringBuilder(lon+"");
        int max = Integer.MAX_VALUE;
        int min = Integer.MIN_VALUE;
        while (sb.charAt(sb.length()-1) == '0')
            sb.delete(sb.length()-1,sb.length());
        sb.reverse();
        long l = Long.parseLong(sb.toString());
        if (is){l = -l;}
        return l > max || l < min ? 0 : (int) l;
    }
}
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页