关于java:7-整数反转

46次阅读

共计 438 个字符,预计需要花费 2 分钟才能阅读完成。

/**
 * 整数反转
 */
public class IntegerReversal {public int reverse(int x) {
        int start = 0;
        String temp = String.valueOf(x);
        if(temp.length() == 1){return x;}
        StringBuilder sb = new StringBuilder();
        if (temp.charAt(0) == '-') {sb.append("-");
            start = 1;
        }
        for (int i = temp.length()-1; i >= start; i--) {sb.append(temp.charAt(i));
        }
        if (Integer.MAX_VALUE < Long.parseLong(sb.toString()) || Integer.MIN_VALUE > Long.parseLong(sb.toString())) {return 0;}
        return Integer.parseInt(sb.toString());
    }
}

欢送探讨交换~~~

正文完
 0