leetcode--8. String to Integer (atoi)

将string转化为int,要求包括:

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.

自己做题思路是,按要求分解成几个小函数,每个函数处理一项工作:

class Solution {
public:

    vector<char> eliminateSpace(string str){
        vector<char> ret;
        for(int i = 0; i<str.length(); i++){
            if(ret.size() > 0 && str[i] == 32) break;
            while(str[i] == 32) i++;
            ret.push_back(str[i]);
        }
        return ret;
    }
    
    vector<char> eliminateUselessChar(vector<char> str){
        vector<char> ret;
        for(int i = 0; i<str.size(); i++){
            if(!((str[i] <= 57 && str[i] >= 48) || str[i] == 43 || str[i] == 45)) break;
            if(ret.size() > 0 && ( str[i] == 43 || str[i] == 45)) break;
            ret.push_back(str[i]);
        }
        return ret;
    }
    
    int vec2int(vector<char> str){
        long ret = 0;
        int i;
        bool sign = true;
        if(str[0] == 45){
            sign = false;
            i = 1;
        } 
        else {
            if(str[0] == 43){
                i = 1;
            } 
            else{
                i = 0;
            }
        }
        for(; i<str.size(); i++){
            ret = sign ? (ret*10 + str[i] - 48) : (ret*10 - (str[i] - 48));
            if(ret > INT_MAX) return INT_MAX;
            if(ret < INT_MIN) return INT_MIN;
        }
        return ret;
    }
    
    int myAtoi(string str) {
        vector<char> a,b;
        int ret;
        a = eliminateSpace(str);
        b = eliminateUselessChar(a);
        if(b.size() == 0) return 0;
        if(b.size() == 1 && (b[0] > 57 || b[0] < 43)) return 0;
        ret = vec2int(b);
        return ret;
    }
        
};


最佳方案:

I think we only need to handle four cases:

  1. discards all leading whitespaces
  2. sign of the number
  3. overflow
  4. invalid input
int atoi(const char *str) {
    int sign = 1, base = 0, i = 0;
    while (str[i] == ' ') { i++; }
    if (str[i] == '-' || str[i] == '+') {
        sign = 1 - 2 * (str[i++] == '-'); 
    }
    while (str[i] >= '0' && str[i] <= '9') {
        if (base >  INT_MAX / 10 || (base == INT_MAX / 10 && str[i] - '0' > 7)) {
            if (sign == 1) return INT_MAX;
            else return INT_MIN;
        }
        base  = 10 * base + (str[i++] - '0');
    }
    return base * sign;
}

step1:跳过所有空格
step2:如果第一位是符号位,设定符号位。这里,通常采取乘以1或-1的方法
step3:while循环遍历连续数字序列。这里判断越界的方法比较好,在乘以10之前,就判断是否越界。
step4:返回







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值