Leetcode-8 ATOI

LeetCode这题的需求非常的怪。这次换了个做法。好像更快一些。


image.png
package leetcode.easy;

/**
 * Created by creat on 2019/7/1.
 */
public class ATOI {


    public static int myAtoi(String str) {
      // trim string to valid number
        char[] trimmedChar = trimStringToValidInt(str);
        if (isTrimmedStringValid(trimmedChar)) {
            return getInteger(trimmedChar);
        }
        return 0;

    }

    public static void main(String[] args) {
        System.out.println(myAtoi("1234"));
        System.out.println(myAtoi("++1234"));
        System.out.println(myAtoi(" "));
        System.out.println(myAtoi("asdfasdfaf"));
        System.out.println(myAtoi("00123"));
        System.out.println(myAtoi("2147483648"));
        System.out.println(myAtoi("1234asdfasdfsaf"));
        System.out.println(myAtoi("+-123"));
        System.out.println(myAtoi(" -42"));
        System.out.println(myAtoi("0-1"));
        System.out.println(myAtoi("123-"));
    }

    private static char[] trimStringToValidInt(String str) {
        if (str == null || str.trim().length() == 0) {
            return "0".toCharArray();
        }
        str = str.trim();
        char[] charArr = str.toCharArray();
        if (!isDigit(charArr[0]) && !isSign(charArr[0])) {
            return "0".toCharArray();
        }
        int begin = getFirstPos(charArr);
        if (begin == -1) {
            return "0".toCharArray();
        }
        int end = getLastPos(charArr, begin);
        char[] toRet = new char[end - begin + 1];
        for (int i = begin; i <= end; i++) {
            toRet[i - begin] = charArr[i];
        }
        return toRet;
    }

    private static int getLastPos(char[] charArr, int beginPos) {
        for (int i = beginPos + 1; i < charArr.length; i++) {
            if (!isDigit(charArr[i])) {
                return i - 1;
            }
        }
        return charArr.length - 1;
    }

    private static int getFirstPos(char[] charArr) {
        int signPos = getFirstSignPos(charArr);
        int digitPos = getFistDigitPos(charArr);
        if (signPos > digitPos) {
            return digitPos;
        }
        if (signPos == -1 && digitPos >= 0) {
            return digitPos;
        }
        return signPos;
    }

    private static int getFistDigitPos(char[] charArr) {
        for (int i = 0; i < charArr.length; i++) {
            if (isDigit(charArr[i])) {
                return i;
            }
        }
        return -1;
    }

    private static int getFirstSignPos(char[] chars) {
        int signPos = -1;
        for (int i = 0; i < chars.length; i++) {
            if (isSign(chars[i])) {
                return i;
            }
        }
        return signPos;
    }

    private static int getInteger(char[] charArr) {
        int result = 0;
        int begin = 0;
        if (isSign(charArr[0])) {
            begin = 1;
        }
        for (int i = begin; i < charArr.length ; i++) {
            if (!isCurrentResultSafe(result, getInt(charArr[i]))) {
                return charArr[0] == '-' ? Integer.MIN_VALUE : Integer.MAX_VALUE;
            }
            result = result * 10;
            result = sum(charArr[0], result, getInt(charArr[i]));
        }
        return result;
    }

    private static int sum(char firstChar, int currResult, int addOn) {
        if (firstChar == '-' ) {
            return currResult - addOn;
        }
        return currResult + addOn;
    }

    private static int getInt(char digit) {
        return digit - '0';
    }

    private static boolean isCurrentResultSafe(int currentResult, int nextDigit) {
        int TENTH_MIN = Integer.MIN_VALUE / 10;
        int TENTH_MAX = Integer.MAX_VALUE / 10;
        if (currentResult == 0) {
            return true;
        }

        if (currentResult < 0 && currentResult > TENTH_MIN) {
            return true;
        }

        if (currentResult > 0 && currentResult < TENTH_MAX) {
            return true;
        }

        if (currentResult < 0 && currentResult == TENTH_MIN && nextDigit <= 0 - Integer.MIN_VALUE % 10) {
            return true;
        }

        if (currentResult > 0 && currentResult == TENTH_MAX && nextDigit <= Integer.MAX_VALUE % 10) {
            return true;
        }
        return false;
    }

    private static boolean isTrimmedStringValid(char[] trimmedArr) {
        if (trimmedArr == null || trimmedArr.length == 0) {
            return false;
        }
        if (trimmedArr.length == 1 && isSign(trimmedArr[0])) {
            return false;
        }
        return true;
    }

    private static boolean isSign(char c) {
        return  c == '+' || c== '-';
    }
    private static boolean isDigit(char c) {
        return c >= '0' && c <= '9';
    }
}

你可能感兴趣的:(Leetcode-8 ATOI)