String to Integer (atoi)

https://oj.leetcode.com/problems/string-to-integer-atoi/

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

spoilers alert... click to show requirements for atoi.

Requirements for atoi:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

解题思路:

考虑了各种情况,比较累。值得注意的就是判断Integer.MAX_VALUE和MIN_VALUE的方法,前面日志讲过。倒数第二位已经大于MAX/10,肯定大于,等于的话,判断最后一位。

public class Solution {

    public int atoi(String str) {

        str = str.trim();

        int result = 0;

        int minus = 1;

        for(int i = 0; i < str.length(); i++){

            if( i == 0 && (str.charAt(0) == '-')){

                minus = -1;

                continue;

            }

            if( i == 0 && (str.charAt(0) == '+')){

                minus = 1;

                continue;

            }

            if(i == 0 && (str.charAt(0) == '+' || str.charAt(0) == '-') && str.length() > 1 && !(str.charAt(1) >= '0' && str.charAt(1) <= '9')){

                return 0;

            }

            if((str.charAt(0) == '+' || str.charAt(0) == '-') && str.length() > 1 && !(str.charAt(1) >='0' && str.charAt(1) <= '9')){

                return 0;

            }

            if((str.charAt(0) == '+' || (str.charAt(0) >='0' && str.charAt(0) <= '9')) && result > Integer.MAX_VALUE / 10 && str.charAt(i) >= '0' && str.charAt(i) <= '9'){

                return Integer.MAX_VALUE;

            }

            if((str.charAt(0) == '+' || (str.charAt(0) >='0' && str.charAt(0) <= '9')) && result == Integer.MAX_VALUE / 10 && str.charAt(i) > '7' && str.charAt(i) <= '9'){

                return Integer.MAX_VALUE;

            }

            if(str.charAt(0) == '-' && result > Integer.MAX_VALUE / 10 && str.charAt(i) >= '0' && str.charAt(i) <= '9'){

                return Integer.MIN_VALUE;

            }

            if(str.charAt(0) == '-' && result == Integer.MAX_VALUE / 10 && str.charAt(i) > '8' && str.charAt(i) <= '9'){

                return Integer.MIN_VALUE;

            }

            if(i >= 0 && !(str.charAt(i) >='0' && str.charAt(i) <= '9')){

                return result * minus;

            }

            result = result * 10 + str.charAt(i) - '0';

        }

        return result * minus;

    }

}

 这里的if比较乱,其实可以用一个finite state machine的方法,也比较死,给str初始化state=0。遇到第一个是'+'或'-',state=1,数字,state=2等等。if state==1,if(state==2),再在分支里对各个state进行处理,和上面的处理其实是一样的。有人在discuss里提出代码如下。

https://oj.leetcode.com/discuss/22247/my-o-n-c-solution-with-finite-state-machine

update 2015/06/10:

二刷,写了个上面有限自动机的方法

public class Solution {

    public int myAtoi(String str) {

        int state = 0, res = 0, minus = 1;;

        for(int i = 0; i < str.length(); i++) {

            char here = str.charAt(i);

            if(state == 0) {

                if(here == ' ') {

                    continue;

                } else if(here == '+' || here == '-') {

                    state = 1;

                    if(here == '-') {

                        minus = -1;

                    }

                } else if(here >= '0' && here <= '9') {

                    res = res * 10 + (int)(here - '0');

                    state =2;

                } else {

                    return 0;

                }

            } else if(state == 1) {

                if(here >= '0' && here <= '9') {

                    state = 2;

                    res = res * 10 + (int)(here - '0');

                } else {

                    return 0;

                }

            } else if(state == 2) {

                if(here >= '0' && here <= '9') {

                    if(minus == 1) {

                        if(res > Integer.MAX_VALUE / 10 || (res == Integer.MAX_VALUE / 10 && (int)(here - '0') > Integer.MAX_VALUE % 10)) {

                            return Integer.MAX_VALUE;

                        }

                    } else {

                        if(res > Integer.MAX_VALUE / 10 || 

                        (res == Integer.MAX_VALUE / 10 && (int)(here - '0') > Integer.MIN_VALUE % 10 * (-1))) {

                            return Integer.MIN_VALUE;

                        }

                    }

                    res = res * 10 + (int)(here - '0');

                } else {

                    return res * minus;

                }

            } else {

                return 0;

            }

        }

        return res * minus;

    }

}

 

你可能感兴趣的:(Integer)