4.23 leetcode -23 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.
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.

好长的题目,字符串转整数,挑战自己不要看hint,那就不看咯。

无非,非数字类的输入(但是输出啥),第一个符号是-或者+,个么正数看心情给‘+’?,个么他有小数点,我要不要考虑呢,真要考虑情况还是很多的吧,写一个看看。

他的用例有毒,空格算了,是我没想到,你给个有字母a的,直接是截断了?

还有更tm奇葩的" +0 123"这个答案不应该是123么,答案是0?!?!

还有呢

假如是"2147483648",答案是2147483647,呵呵了,超过量程算最大?

真是日了狗了,投机取巧了,用了个long

class Solution {
public:
    int atoi(const char *str) {
        bool fuflag = false;//0 - zhengshu true - fushu
        long number = 0;
        int nloc = 0;
        bool firstflag = false;
        while(str[nloc] != '\0')
            {
            if(str[nloc] == ' ' && firstflag == false)
                ;
            else if(str[nloc] == '-' && firstflag == false)
                {
                fuflag = true;
                firstflag = true;
            }
            else if(str[nloc] == '+' && firstflag == false)
                {
                fuflag = false;
                firstflag = true;
            }
            else if(str[nloc] < 58 && str[nloc] >= 48)
                {
                number = number*10 - (str[nloc] - 48);
                firstflag = true;
            }
            else
                break;
            nloc ++;
        }
        if(number < -2147483647 && fuflag == false)//正数超量程
            return 2147483647;
        if(number < -2147483648 && fuflag == true)//负数超量程
            return 2147483648;
        if(fuflag == false)
            number = 0 - number;
        return number;
    }
};


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值