2. String to Integer(atoi)

来源:互联网 发布:淘宝首页店招尺寸 编辑:程序博客网 时间:2024/06/10 09:23
8. 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 requirement up front.


update (2015-02-10):
the signature of the C++ function had been updated. if you still see your function signature accepts a const char * argument, please click the reload button
to reset your code definition.


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 interpretes them as a numerical value.


public class Solution {
    public int myAtoi(String str) {
        if (str==null||str.length()==0)
        return 0;
        str = str.trim();
        char firstChar = str.charAt(0);
        int sign = 1, start = 0, len = str.length();
        long sum = 0;
        if(firstChar == '+'){
            sign = 1;
            start++;
        }else if (firstChar == '-'){
            sign = -1;
            start++;
        }
        for(int i= start; i<len; i++){
           if(!Character.isDigit(str.charAt(i))){
                return (int) sum*sign;
           }
           sum = sum * 10 + str.charAt(i) - '0';
           if (sign==1 && sum > Integer.MAX_VALUE)
               return Integer.MAX_VALUE;
           if (sign==-1 && (-1)*sum < Integer.MIN_VALUE)
               return Integer.MIN_VALUE;
       }
       return (int) sum * sign;
    }
}


another solution:
a desirable solution does not require any assumption on how the language works. in each step we are appending a digit to the number by doing a multiplication
and addition. if the current number is greater than 214748364, we know it is going to overflow. on the other hand, if the current number is equal to 214748364, we know
that it will overflow only when the current digit is greater than or equal to 8. remember to also consider edge case for the smallest number, -2147483648 (-2^31)
public class Solution {
    private static final int maxDiv10 = Integer.MAX_VALUE/10;
    public int myAtoi(String str){
     int i =0, n=str.length();
     while(i<n&&Character.isWhitespace(str.charAt(i))) i++;
     int sign = 1;
     if (i<n && str.charAt(i) == '+'){
        i++;
     }else if(i<n && str.charAt(i) == '-'){
        sign = -1;
        i++;
     }
     int num = 0;
     while (i<n && Character.isDigit(str.charAt(i))){
        int digit = Character.getNumericValue(str.charAt(i));
        if(num>maxDiv10 || num == maxDiv10 && digit >= 8){
             return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
        }
        num = num * 10 + digit;
        i++;
     }
     return sign * num;
   }
}



0 0