整数反转

class Solution {
    public static void main(String[] args){
        int target = 7654321;

        int result = reverse(target);
        System.out.println(result);
    }

    public static int reverse(int x) {
        int pop;
        int result =0;
        while(x!=0){
        //第一步 pop最后一位数字
        pop=x%10;

        //第二步 x=x/10;
        x=x/10;

        //第三步 result=result*10 + pop;
        // 如果 result 的值大于 Integer.MAX_VALUE / 10 ,那么一定会溢出
            // 如果 result 的值等于 Integer.MAX_VALUE / 10,那么 pop 的值如果大于 Integer.MAX_VALUE % 10 也会溢出
            // 相反的,result 的值小于 Integer.MIN_VALUE / 10 ,那么一定会溢出
            // 如果 result 的值等于 Integer.MIN_VALUE / 10,那么 pop 的值如果小于于 Integer.MIN_VALUE % 10 也会溢出

            if(result>Integer.MAX_VALUE/10||(result==Integer.MAX_VALUE/10&&pop>Integer.MAX_VALUE%10)){
                result=0;
                return result;
            }
            if(result

你可能感兴趣的:(整数反转)