7. 整数反转

7. 整数反转

package 字符串;

public class 整数反转 {
    public static void main(String[] args) {
        整数反转 o = new 整数反转();
        System.out.println(o.reverse2(-2147483412));
//        System.out.println(Integer.MAX_VALUE);//2147483647
//        System.out.println(Integer.MIN_VALUE);//-2147483648
    }


    // 常规方法,先把数字转成字符串,然后反转,反转后再校验数字的合法性
    public int reverse(int x) {
        int result = 0;
        boolean flag = false;
        if (x < 0) {
            x = -x;
            flag = true;
        }
        String s = String.valueOf(x);
        char[] c = s.toCharArray();
        int i = 0;
        int j = c.length - 1;
        while (i < j) {
            char temp = c[i];
            c[i] = c[j];
            c[j] = temp;
            i++;
            j--;
        }
        // 翻转后的数字
        String r = String.valueOf(c);
        // 判断是否有效
        if ((r.length() == 10 && !isEffective(flag, r)) || r.length() > 10) {
            return 0;
        }
        result = Integer.parseInt(r);
        if (flag) {
            result = -result;
        }
        return result;
    }

    // flag标志正负数,ture是负数
    public boolean isEffective(boolean flag, String r) {
        String compare = null;
        if (flag) {
//            compare = "2147483648";
            String sMin = String.valueOf(Integer.MIN_VALUE);
            compare = sMin.substring(1, sMin.length());
        } else {
            compare = String.valueOf(Integer.MAX_VALUE);
//            compare = "2147483647";
        }
        // 2143847412
        // 2147483648
        for (int i = 0; i < r.length(); i++) {
            if (r.charAt(i) == compare.charAt(i)) {
                continue;
            } else if (r.charAt(i) > compare.charAt(i)) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }


    // 按位截取反转自己实现
    public int reverse1(int x) {
        int result = 0;
        while (x != 0) {
            int k = x % 10;
            x = x / 10;
            if (result > Integer.MAX_VALUE / 10 || (result == Integer.MAX_VALUE / 10 && k > 7)) return 0;
            if (result < Integer.MIN_VALUE / 10 || (result == Integer.MIN_VALUE / 10 && k < -8)) return 0;
            result = result * 10 + k;
        }
        return result;
    }


    // 按位截取反转
    public int reverse2(int x) {
        int rev = 0;
        while (x != 0) {
            //int的最大值是xxx7,最小值是-xxx8,模拟栈的操作,例如:8753,取第一个数,利用%,剩下的就是除以10取整
            int k = x % 10;
            x = x / 10;
            //正数,当塞到剩最后一位时判断,发现大了肯定会溢出,等于的时候,最后一位不要大于7,如果溢出就返回0
            //当获取的位数超过了最长位数-1,需要小心是否会溢出的问题了
            if (rev > Integer.MAX_VALUE / 10 || (rev == Integer.MAX_VALUE && k > 7)) return 0;

            //负数
            if (rev < Integer.MIN_VALUE / 10 || (rev == Integer.MIN_VALUE / 10 && k < -8)) return 0;
            //将该数追加到rev后端
            rev = rev * 10 + k;
        }
        return rev;
    }
}

..

class Solution {
    public int reverse(int x) {
        // 121939423942394
        int rev = 0;
        while (x != 0) {
            if (rev < Integer.MIN_VALUE/10 || rev > Integer.MAX_VALUE/10) {
                return 0;
            } else {
                rev = rev * 10 + x % 10;
                x = x / 10;
            }
        }
        return rev;
    }
}
原文地址:https://www.cnblogs.com/guoyu1/p/15625436.html