LeetCode 008. String to Integer (atoi)

来源:互联网 发布:上海数据港待遇如何 编辑:程序博客网 时间:2024/05/02 15:14

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.







这道题咋一看感觉不难,基本功能很好实现,但是这里面需要考虑的问题确实非常可观的。

这道题最大的特色就在于各种临界条件。

关于32 bit 的int型数据范围是-2147483648 ~ 2147483647,这个也是其中需要考虑的问题之一。

除此之外LeetCode上还给出一些特殊的测试用例,如:“    +- 342”、“+342 3435”、“+2147483648”、“-2147483648”、“-1”、“”、“   -00003435”、“000466 899”

诸如此类的测试用例还有很多。

class Solution {public:    int atoi(const char *str)     {        if(str == NULL || *str == '\0')            return 0;        double result = 0.0;        int true_result = 0;        bool negatime = false; //负数出现次数        bool negative = false; //判断是否是负数        bool nonezero = true;  //首个非空字符是否是0,如果首个非空字符为0,那么则应舍弃掉。        bool numexist = false; //如果已经出现数字了,若再出现空格等其他字符则应该返回0,测试用例:+0 123        while(*str)        {                        if(*str<='9' && *str >='0')            {                numexist = true;                if(nonezero)  //首个非空字符为0,str++                {                    if(*str != '0')                        nonezero = false;                    else                        str ++;                }                else                {                    result += (*str - '0');                    result *= 10;                    str ++ ;                             }            }            else if(*str == ' ')            {                if(numexist)   //之前已经出现过数字,再出现空格,则将已有结果输出                    break;                if(negatime)   //之前已经出现过正负号,再出现空格, 则将已有结果输出                    break;                str ++;            }            else if(*str == '-' || *str == '+')            {                 if(numexist)   //之前已经出现数字,再出现正负号,则将已有结果输出                    break;                if(negatime)                    return 0; //之前已经出现过正负号,再出现正负号,则判定输入不是数字                else                 {                    negatime = true;                    if(*str == '-')                        negative = true;                }                str ++;            }            else      //此时出现了其他的字符,将已经得到的结果返回                break;        }        result /= 10;        //边界处理        if(negative)        {            if(result > 2147483648)                true_result = -2147483648;            else                true_result = 0-result;        }        else        {            if(result > 2147483647)                true_result = 2147483647;            else                 true_result = result;        }        return true_result;    }};







0 0
原创粉丝点击