LeetCode 8:String to Integer (atoi)

来源:互联网 发布:网络教育排行榜 编辑:程序博客网 时间:2024/05/24 06:37

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.


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.

实现atoi函数(字符串转换为int型整数)

提示:仔细考虑所有可能的输入情况,如果你想挑战自己,尝试在不看下面函数要求的情况下自己给出所有可能的输入情况。

注意:这说明这个问题是模糊的(不规定输入格式),你必须要考虑到所有的输入情况。


atoi函数要求:(注意,如果你想挑战自己的话,请不要看这段话)

函数首先必须丢弃所有开头的空格(即" "),直到找到一个非空格字符。然后,判断该数字是正数还是负数,并读取接下来的所有数字,直到一个非数字字符。之后,将这串数字字符解释为一个数值。

输入的字符串可能在你读取的数字字符之后有其他字符(例如"123abb"),它们在这个函数中没有任何作用,你应当忽略它们。

如果第一个非空格字符不是一个合法的字符(即不是以"+"、"-"或0~9数字开头)、字符串为空,或者字符串只包含空格,那么将不执行转换(即函数应当简单的返回0)。

如果没有执行合法的转换,那么应当返回0。如果字符串所代表的正确的数值溢出,即超过了INT型整数的范围,那么应当返回INT_MAX(2147483647,在正数情况下)或INT_MIN(-2147483648,在负数情况下)。


好吧,这道题真是把我整得要死,因为我忘记有提示了。。。其实读取数字字符的问题还好解决,我并没有消除字符串中的空格,而是找到第一个数字字符,然后判断它之前的所有字符是否都是有效的。关键是判断整数溢出的问题让我纠结了好久,尝试百度了下,也没有发现什么特别好的方法。我自己的方法是在每次执行运算之后(无论是加还是乘)都再倒着执行一次(即把乘出来的结果再除回去,加出来的结果再减回去),判断是否与原来的数相同,如果不同则说明发生了溢出。

class Solution {public:    int myAtoi(string str) {        if(str.length()==0) return 0;        int flag=1,temp=0;        int ans=0;        if(str.find_first_of("0123456789")!=string::npos) temp=str.find_first_of("0123456789");        else return 0;        if(temp>=2)        {            for(int i=0;i<temp-1;i++)                if(str[i]!=' ') return 0;            if(str[temp-1]=='-') flag=-1;            else if(str[temp-1]=='+'||str[temp-1]==' ') flag=1;            else return 0;        }        if(temp==1)        {            if(str[temp-1]=='-') flag=-1;            else if(str[temp-1]=='+'||str[temp-1]==' ') flag=1;            else return 0;        }        while(str[temp]>='0'&&str[temp]<='9'&&temp<str.length())        {            cout<<"str[temp]="<<str[temp]<<endl;            int test1=ans*10;            if(ans!=test1/10+test1%10)            {                if(flag==1) return 2147483647;                else return -2147483648;            }            ans*=10;            int test=str[temp]-'0';            if((unsigned)ans+(unsigned)test>2147483647)            {                if(flag==1) return 2147483647;                else return -2147483648;            }            ans+=test;            temp++;            cout<<"ans="<<ans<<endl;        }        return ans*flag;    }};


0 0
原创粉丝点击