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).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值