[leetcode] String to Integer (atoi)


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.

tag: math, string

9/25/2015 update, a c++ approach

//heading and trailing spaces
//overflow? return the MAX_INT or MIN_INT
//empty string?
//space in the middle?
//illegal number?
//is +5 legal?
//is -0005 legal?
//is -0 legal?
//if there are non-digit character in str, stop it and convert previous characters to num
class Solution {
public:
    int myAtoi(string str) {
        int start = 0;
        int end = str.size() - 1;
        long ans = 0;
        int flag = 1;
        //deleting heading and trailing spaces
        while(start < str.size() && str[start] == ' '){
            start++;
        }
        
        while(end >= 0 && str[end] == ' '){
            end--;
        }
        if(end < start) return 0;
        
        if(str[start] == '-'){
            flag = -1;
            start++;
        }
        else if(str[start] == '+'){
            flag = 1;
            start++;
        }
        while(start != end + 1){
            char c = str[start];
            if(isDigit(c) == false){
                break;
            }
            ans = ans * 10 + c - '0';
            //check overflow
            if(ans > (int)0x7fffffff && flag == 1){
                return (int)0x7fffffff;
            }
            else if(ans > (long)0x80000000 && flag == -1){
                return (int)0x80000000;
            }
            start++;
        }
        return (int)(ans * flag);
    }
    bool isDigit(char c){
        return c <= '9' && c >= '0';
    }
};

 

虽然被leetcode标为简单题,但有很多细节需要考虑。

1. 对overflow的判断– 在数字乘以10之前进行判断;

2. 判断符号;

3. 忽略符号出现前的空白符;

4. 忽略符号出现后的非数字字符。

class Solution {
public:
    int atoi(const char *str) {
        int flag = 1;
        int start = 0;
        while(str[start] == ' '){//ignore whitespace
            start++;
        }
        if(str[start] == '-'){
            flag = -1;
            start++;
        }
        else if(str[start] == '+'){
            flag = 1;
            start++;
        }
        int end = start;
        while(str[end] != '\0' && str[end] <= '9' && str[end] >= '0'){
            end++;
        }
        return convert(str, start, end, flag);
    }
    int convert(const char * str, int start, int end, int flag){
        int re = 0;
        int digit;
        for(int i = start; i < end; i++){
            digit = str[i] - '0';
            if(re > 214748364 || (re == 214748364 && (digit > 8 || (digit == 8 && flag == 1)))){//overflow
                if(flag == 1){
                    return 0x7FFFFFFF;
                }
                else{
                    return 0x80000000;
                }
            }
            re = re * 10 + digit;
        }
        return re * flag;
    }
    
};

Selection_013

 

 

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.