String to Integer (atoi)

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.

其实就是让实现atoi函数,思路也很简单,将函数参数的字符串的前后空格去除掉,然后再做判断,合法的字符是+-0-9,还需要判断+-的个数,而且在这里并不需要去做诸如00123的字符串合法性判断,只需要按计算方法做就行。

从去除空格后的字符串依次遍历,如果数据不合法就退出,通过ans = 0来记录结果,如果是+-就记录它们的个数,并且记录是否出现‘-’,在最后输出时需要与INT_MAX, INT_MIN做比较,因为返回是int型,不能超出int表示的最大值。(ans则声明为long long)

int atoi(const char *str) {
        int len = strlen(str);
        set<char> sets;
        sets.insert('0');
        sets.insert('1');
        sets.insert('2');
        sets.insert('3');
        sets.insert('4');
        sets.insert('5');
        sets.insert('6');
        sets.insert('7');
        sets.insert('8');
        sets.insert('9');
        
        long long ans = 0;
        int a = 0, b = 0;  //记录+-的个数
        bool isfalse = false;  //判断是否出现了-
        int i = 0, j = len-1;
        while(str[i] == ' '){
            i++;
            if(i == len)
                break;
        }
        
        while(str[j] == ' '){
            j--;
            if(j < 0)
            break;
        }
        
        for(; i <= j; i++){
            if(sets.count(str[i])){
                ans *= 10;
                ans += str[i] - '0';
                continue;
            }
                
            if(str[i] == '+')
                a++;
            else if(str[i] == '-'){
                isfalse = true;
                b++;
            }else
                break;
                
            if(a+b>1)
                break;
        }
        if(isfalse)
            ans = -ans;
        if(ans > INT_MAX)
            ans = INT_MAX;
        else if(ans < INT_MIN)
            ans = INT_MIN;
            
        return ans;
    }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值