[LeetCode]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.

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.


題目希望答題者在沒有給出具體的要求,只說明讓把string轉換爲int型的情況下,考驗答題者能不能夠全面去考慮這道題,只要把各方面考慮到這道題其實不難。


下面是我總結的幾個考察點:

  1. 輸入爲空字符串時;
  2. 字符串前有空格時;比如“     324132”
  3. 字符串前有+-號的時候,主要不能出現+-好只能出現一個,並只能出現一次,根據其確定輸出是正數還是負數;
  4. 越界問題,我的代碼裏用的是我自己的土辦法,當然也可以把result定義long long類型,然後直接用起與max=0x7fffffff或min=0x80000000;
  5. 當讀取時中間突然輸入一個非數字字符,則返回已讀到的數字;比如1234a123;
  6. 當越上界則返回上屆0x7fffffff,當越下界時則返回下界0x80000000


class Solution {
public:
    int myAtoi(string str) 
    {
        int l=str.length();
        if(l==0)
        {
            return 0;
        }
        int result=0;
        int i=0,a=1;//a用來標記正負
        bool first=false;
        while(str[i]==' ') 
        {
            i++;
        }
        while(str[i]=='+'||str[i]=='-')//用來確認有幾個+-符號,如果超過一個則返回0

        {
            if(str[i]=='+')
            {
                if(first) //如果是第二個+-號,則返回0
                {
                    return 0;
                }
                first=true;
            }
            else if(str[i]=='-')
            {
                if(first) 
                {
                    return 0;
                }
                first=true;
                a=-1;
            }
            i++;
        }
        while(str[i]!='\0')
        {
            if(str[i]<'0'||str[i]>'9')
            return result;//如果出現非數字符號,則返回當前讀取的數字
            if(result>214748364||(result==214748364&&(str[i]-'0')>7))//越上界
            {
                return 2147483647;//如果越界則返回最大int值
            }
            else if(result<-214748364||(result==-214748364&&(str[i]-'0')>8))//越下界

            {
                return -2147483648;//如果越界則返回最小int值
            }
            result=result*10+a*(str[i]-'0');//a標記的是正負號
            i++;
        }
        return result;
    }
};






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章