給出一個 32 位的有符號整數,你須要將這個整數中每位上的數字進行反轉。

 

package com.gupao.jay.pattern.singleton;

/**
 * @Author JAY
 * @Date 2019/6/2 9:50
 * @Description 給出一個 32 位的有符號整數,你須要將這個整數中每位上的數字進行反轉。
 *
 * 示例 1:
 *
 * 輸入: 123
 * 輸出: 321
 *  示例 2:
 *
 * 輸入: -123
 * 輸出: -321
 * 示例 3:
 *
 * 輸入: 120
 * 輸出: 21
 * 注意:
 *
 * 假設咱們的環境只能存儲得下 32 位的有符號整數,則其數值範圍爲 [−231,  231 − 1]。請根據這個假設,若是反轉後整數溢出那麼就返回 0。
 **/
public class LuTest {

    public static void main(String[] args) {
        LuTest test = new LuTest();
        int x = -2147483412;
        System.out.println(test.reverse(x));
    }

    public int reverse(int x) {
        String str = String.valueOf(x);
        int pow = (int) Math.pow(2, 31);
        int leftLimit = pow * (-1);
        int rightLimit = pow - 1;
        System.out.println(leftLimit);
        System.out.println(rightLimit);
        char[] chars = str.toCharArray();
        int limit = 0;
        char[] newChars = null;
        if ('-' == chars[0]){
            newChars = new char[chars.length - 1];
            for (int i = 1; i < chars.length; i++){
                newChars[chars.length - 1 - i] = chars[i];
            }
            limit = leftLimit * (-1);
            return validateTargetNumber(newChars,limit) * (-1);
        }else {
            newChars = new char[chars.length];
            for (int i = 0; i < chars.length; i++){
                newChars[chars.length - 1 - i] = chars[i];
            }
            limit = rightLimit;
            return validateTargetNumber(newChars,limit);
        }
    }

    private int validateTargetNumber(char[] newChars, int limit) {
        String targetStr = String.valueOf(newChars);
        char[] rs = filterZero(targetStr);
        boolean isOut = checkOutInt(limit, rs);
        if (isOut){
            return 0;
        }
        String valueOf = String.valueOf(rs);
        return Integer.valueOf(valueOf);
    }

    private char[] filterZero(String targetStr) {
        if (targetStr.length() == 1 && "0".equals(targetStr)){
            char[] chars = new char[1];
            chars[0] = '0';
            return chars;
        }
        //過濾前面位置爲0的數字
        char[] number = targetStr.toCharArray();
        //前面位置是0的長度
        int zeroLength = 0;
        for (int i = 0; i < number.length; i++){
            if ('0' != number[i]){
                break;
            }
            zeroLength++;
        }
        char[] rs = new char[number.length - zeroLength];
        for (int i = zeroLength; i < number.length; i++){
            rs[i - zeroLength] = number[i];
        }
        return rs;
    }

    private boolean checkOutInt(int limit, char[] rs) {

        //將過濾掉0以後的數字進行越界判斷
        String limitStr = String.valueOf(limit);
        if (rs.length > limitStr.length()){
            //越界
            return true;
        }
        if (rs.length < limitStr.length()){
            //未越界
            return false;
        }
        //若是數字長度相等,則判斷是否越界
        char[] limitChars = limitStr.toCharArray();
        for (int j = 0; j < limitChars.length; j++){
            //結果數字每一個位置上的數字必須小於等於limit的每一個位置上的數字
            if(rs[j] > limitChars[j]){
                //越界
                return true;
            }else if(rs[j] < limitChars[j]){
                //不越界
                return false;
            }
        }
        return false;
    }

}
相關文章
相關標籤/搜索