LeetCode 008 StringToInteger(atoi)

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


class Solution {
public:
    int myAtoi(string str) {
    }
};



解题思路:
  • 自己的解题思路
很简单,就是依次转存。但是, 我测试不通过了 4 ,只考虑 string 为空的情况,其他情况都没有考虑。可见,对于程序的健壮性这点,以及程序测试这块,我的水平很差,需要继续不断注意训练。
  • 别人的解题思路
we now need to compute number = number * base + digval, but we need to know if overflow occured.  This requires a tricky pre-check. “
程序 运用一个与头文件类似的技巧,就是提前检查。比较好。而且使用 INT_MAX, 具有可移植性。
程序 有点类似我的想法。
学习收获:
  • 还是一个对于程序的健壮性考虑。由于题目练得比较少,所以这方面比较薄弱。
  • 第一次,没有考虑正负号的处理;
  • 第二次,没有考虑空格的处理;
  • 第三次,没有考虑越界的情况;所以用了 long 进行处理
  • 第四次,连 long 也存不下了,所以必须提前对程序进行判断。
  • string format: [whitespace] [sign] [0] [x] [digits/letters]
官方默认有效的 string 格式是这样的。如果,出现“ +-11 ”,应该返回 0
  • 熟悉使用 str.find_first_not_of();
附件:程序
1、自己的程序:
class Solution
{
    public:
    int myAtoi(string str)
    {
        long long res = 0;
        if(0 == str.size())
        {
            return res;
        }
        auto i = str.begin();
        while(i != str.end() && isspace(*i))
        {
            ++i;
        }
        if(i == str.end())
        {
            return res;
        }
        bool flag = true;
        int sign = 1;
        for(; i != str.end(); ++i)
        {
            if(flag&&*i == '-')
            {
                sign = -1;
                flag = false;
                continue;
            }
            else if(flag&&*i == '+')
            {
                flag = false;
                continue;
            }
            if(*i >= '0'&&*i <= '9')
            {
                res = res * 10 + (*i - '0');
            }
            else
            {
                break;
            }
            if(res > INT_MAX)
            {
                break;
            }
        }
        res = sign*res;
        if(res > INT_MAX)
        {
            res = INT_MAX;
        }
        else if(res < INT_MIN)
        {
            res = INT_MIN;
        }
        return static_cast<int>(res);
    }
};


2、别人的程序
这是比较接近官方文件的解答。

int myAtoi(string str)
{
    int ret = 0, sign = 1, i = str.find_first_not_of(' '), base = INT_MAX / 10;
    if(str[i] == '+' || str[i] == '-') sign = str[i++] == '+' ?: -1;
    while(isdigit(str[i]))
    {
        if(ret > base || (ret == base && str[i] - '0' > INT_MAX % 10))          return sign > 0 ? INT_MAX : INT_MIN;
        ret = 10 * ret + (str[i++] - '0');
    }
    return sign * ret;
}

int myAtoi(string str)
{
    long result = 0;
    int indicator = 1;
    for(int i = 0; i < str.size();)
    {
        i = str.find_first_not_of(' ');
        if(str[i] == '-' || str[i] == '+')
            indicator = (str[i++] == '-')? -1 : 1;
        while('0' <= str[i] && str[i] <= '9')
        {
            result = result * 10 + (str[i++] - '0');
            if(result*indicator >= INT_MAX) return INT_MAX;
            if(result*indicator <= INT_MIN) return INT_MIN;
        }
        return result*indicator;
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值