乐趣区

关于java:7-整数反转

/**
 * 整数反转
 */
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());
    }
}

欢送探讨交换~~~

退出移动版