美文网首页
8. String to Integer (atoi)

8. String to Integer (atoi)

作者: wtmxx | 来源:发表于2018-02-02 21:02 被阅读0次
    class Solution {
        public int myAtoi(String str) {
            int len = str.length();
            if(len==0){
                return 0;
            }
            long result=0;
            int i = 0;
            int scount = 0;
            boolean negative = false;
            while(str.charAt(i)==' '&&i<len-1){
                i++;
                scount++;
            }
            if(str.charAt(i)=='-'){
                if(len==scount+1)
                    return 0;
                negative = !negative;
                i++;
            }else if(str.charAt(i)=='+'){
                if(len==scount+1)
                    return 0;
                i++;
            }
            while(str.charAt(i)=='0'&&i<len-1){
                i++;
            }
    
            for(;i<len;i++){
                if(str.charAt(i)>='0'&&str.charAt(i)<='9'){
                    result*=10;
                    result += Character.digit(str.charAt(i),10);
                    if(result>(long)Integer.MAX_VALUE+1||(result==(long)Integer.MAX_VALUE+1&&!negative)){
                        return negative?Integer.MIN_VALUE:Integer.MAX_VALUE;
                    }
                }else{
                    break;
                }
            }
            return negative?-(int)result:(int)result;
        }
    }
    

    看一下java.lang.Integer的实现(并不是本题的解)

    public static int parseInt(String s, int radix)
                    throws NumberFormatException
        {
            /*
             * WARNING: This method may be invoked early during VM initialization
             * before IntegerCache is initialized. Care must be taken to not use
             * the valueOf method.
             */
    
            if (s == null) {
                throw new NumberFormatException("null");
            }
    
            if (radix < Character.MIN_RADIX) {
                throw new NumberFormatException("radix " + radix +
                                                " less than Character.MIN_RADIX");
            }
    
            if (radix > Character.MAX_RADIX) {
                throw new NumberFormatException("radix " + radix +
                                                " greater than Character.MAX_RADIX");
            }
    
            int result = 0;
            boolean negative = false;
            int i = 0, len = s.length();
            int limit = -Integer.MAX_VALUE;
            int multmin;
            int digit;
    
            if (len > 0) {
                char firstChar = s.charAt(0);
                if (firstChar < '0') { // Possible leading "+" or "-"
                    if (firstChar == '-') {
                        negative = true;
                        limit = Integer.MIN_VALUE;
                    } else if (firstChar != '+')
                        throw NumberFormatException.forInputString(s);
    
                    if (len == 1) // Cannot have lone "+" or "-"
                        throw NumberFormatException.forInputString(s);
                    i++;
                }
                multmin = limit / radix;
                while (i < len) {
                    // Accumulating negatively avoids surprises near MAX_VALUE
                    digit = Character.digit(s.charAt(i++),radix);
                    if (digit < 0) {
                        throw NumberFormatException.forInputString(s);
                    }
                    if (result < multmin) {
                        throw NumberFormatException.forInputString(s);
                    }
                    result *= radix;
                    if (result < limit + digit) {
                        throw NumberFormatException.forInputString(s);
                    }
                    result -= digit;
                }
            } else {
                throw NumberFormatException.forInputString(s);
            }
            return negative ? result : -result;
        }
    

    相关文章

      网友评论

          本文标题:8. String to Integer (atoi)

          本文链接:https://www.haomeiwen.com/subject/eptyzxtx.html