@@ -0,0 +1,1531 @@
+package fundamentals.string.lc008_stringtointeger;%0A%0A/**%0A * Implement atoi to convert a string to an integer.%0A * Hint: Carefully consider all possible input cases.%0A * If you want a challenge, please do not see below and ask yourself what are the possible input cases.%0A */%0Apublic class Solution %7B%0A%0A    public int myAtoi(String str) %7B%0A        if (str == null) %7B%0A            return 0;%0A        %7D%0A%0A        char%5B%5D chars = str.toCharArray();%0A        if (chars.length == 0) %7B%0A            return 0;%0A        %7D%0A%0A        // Trap-1: Trim leading whitespace%0A        int j = 0;%0A        while (chars%5Bj%5D == ' ') %7B%0A            j++;%0A            if (j %3E= chars.length) %7B%0A                return 0;%0A            %7D%0A        %7D%0A%0A        // Trap-2: Plug/minus flag%0A        boolean isNeg = false;%0A        if (chars%5Bj%5D == '+') %7B%0A            j++;%0A        %7D else if (chars%5Bj%5D == '-') %7B%0A            j++;%0A            isNeg = true;%0A        %7D%0A%0A        // Perform conversion%0A        int i = 0;%0A        for (; j %3C chars.length; j++) %7B%0A            // Trap-3: Ignore following invalid char%0A            if (chars%5Bj%5D %3C '0' %7C%7C chars%5Bj%5D %3E '9') %7B%0A                break;%0A            %7D%0A%0A            // Trap-4: Overflow%0A            if (!isNeg && (Integer.MAX_VALUE - (chars%5Bj%5D - '0')) / 10 %3C i) %7B%0A                return Integer.MAX_VALUE;%0A            %7D%0A            if (isNeg && (Integer.MIN_VALUE + (chars%5Bj%5D - '0')) / -10 %3C i) %7B%0A                return Integer.MIN_VALUE;%0A            %7D%0A%0A            i = i * 10 + (chars%5Bj%5D - '0');%0A        %7D%0A        return isNeg ? -i : i;%0A    %7D%0A%0A%7D%0A

leetcode-008 string to integer