LeetCode第八题——string to integer

问题描述
Implement atoi which converts a string to an integer.

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.

Note:

Only the space character ’ ’ is considered as whitespace character.
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.
解法思路
初始的想法是将字符串中的数字字符部分复制到另一个字符串,然后用atoi,但之后发现由于字符串大小不知道,所以这种思路不行。网上查找相关信息,发现可以直接用s[i]-48,将数字字符直接转化为数字,那就可以直接将整体数字提取出来。但提交的时候总是告诉我res过大,需要在提取数字的时候加一个判断条件,超过INT范围后,直接return,可解决这个问题。
C++代码

class Solution {
public:
    int myAtoi(string str) {
        long long res=0;
        int n=str.size();
         for(int i=0;i<n;++i)
         {
             if(str[i]==' ') continue;
             else if(str[i]=='-')//空格过后第一个为+/-,存储接下来所有的数字型字符
             {
                ++i;
                 while(i<n&&(str[i]>='0'&&str[i]<='9'))
                 {
                     res=res*10+(str[i]-48);
                     if((-res)<INT_MIN) return INT_MIN;//加入判断条件,如果超过范围,直接return
                     ++i;
                 }
                 res=-res;
                 break;//已经得到所有数字
             }
             else if(str[i]=='+')
             {
                 ++i;
                 while(i<n&&(str[i]>='0'&&str[i]<='9'))
                 {
                     res=res*10+(str[i]-48);
                     if(res>INT_MAX) return INT_MAX;
                     ++i;
                 }
                 break;
             }
             else if(str[i]>='0'&&str[i]<='9')
             {
                 res=res*10+(str[i]-48);
                 ++i;
                 while(i<n&&(str[i]>='0'&&str[i]<='9'))
                 {
                     res=res*10+(str[i]-48);
                     if(res>INT_MAX) return INT_MAX;
                     ++i;
                 }
                 break;
             }
             else
             {
                 return 0;//空格之后的第一个字符不是+/-
             }
         }
        if(res<INT_MIN) return INT_MIN;
        else if(res>INT_MAX) return INT_MAX;
        else
        {
            return res;
        }
    }
};

结果分析
Runtime: 20 ms, faster than 95.39% of C++ online submissions for String to Integer (atoi).
Memory Usage: 14.3 MB, less than 97.17% of C++ online submissions for String to Integer (atoi).

猜你喜欢

转载自blog.csdn.net/weixin_43487878/article/details/87885076