字符串转int型atoi函数-leetcode

来源:互联网 发布:国际淘宝网官方网站 编辑:程序博客网 时间:2024/05/19 02:19

昨天在leetcode上面刷题,遇到了字符串转整型的一道题,贴到这里。

原题:

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.

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.

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.

Subscribe to see which companies asked this question

什么意思呢?就是输入一个字符串,转为整型,当然啦,这个字符串是有意义的,你总不能输入"abc"让我转成整型吧。那么,你就需要考虑多种情况了。

首先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什么意思呢?就是输入的字符串里面可能带正负号,也可能有空格。但是这些都是合法的,可以转为为整型。比如“+123”,“123”结果就是123。

“-123”就是-123。“           -123”这个字符串结果就是-123。

接下来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.这句话就是字符串里面可以有其他字符,但是有条件的。比如“     -123a123”结果就是-123,但是“      b123”就是非法的。意思就是若想转为整型,字符串除空格外,第一项必须是“+“,”-”或者数字。除此之外,连续的数字后面若是有其他非数字字符,忽略掉,不影响结果。就是这样的。“123abc”结果就是123。“1234a12”结果就是1234.

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.这句话就是非法情况返回值为0。但是考虑整型能表示的最大值。如果溢出,我们就返回INT_MAX或者INT_MIN。ok,可以写代码了。代码如下:

 int myAtoi(string str) {        int begin=0;        int end=0;        long long Iteger=0;        int i=0;        if(str.size()<=0) return 0;        for(i=0;i<str.size();i++)        {            if(str[i]>='0'&&str[i]<='9'||str[i]=='+'||str[i]=='-')            {                begin=i;                break;            }            else if(str[i]!=' ')            {                return 0;            }        }        end=begin;        for(i=begin+1;i<str.size();i++)        {            if(str[i]<'0'||str[i]>'9')            {                end=i;                break;            }        }        if(i==str.size()) end=str.size()-1;        for(i=begin;i<=end;i++)        {            if(str[i]>='0'&&str[i]<='9')            {                int tmp=str[i]-'0';                Iteger=Iteger*10+tmp;             if(Iteger-1>INT_MAX)            {                break;            }            }        }        if(str[begin]=='-')        {            if(Iteger-1>=INT_MAX)            return INT_MIN;            else            return -Iteger;        }        else        {            if(Iteger>=INT_MAX)            return INT_MAX;            else            return Iteger;        }            }

需要考虑的情况还蛮多的。好在最后AC了。心累!


0 0
原创粉丝点击