【Leetcode】8. String to Integer (atoi) 字符串转整型

1. 题目

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.

2. 思路

功能相对简单,但是题目描述和最有的判定要求太模糊。
比如标准答案不考虑8进制和16进制的数字字面形式的处理。
对于前导0进行持续忽略处理。
注意1:符号位+-
注意2:越界的字面返回最大值或者最小值
注意3:021不是8进制的17,而是十进制的21.
注意4:0x12不是16进制的18,而是0.

3. 代码

耗时:16ms

class Solution {
public:
    int myAtoi(string str) {
        int len = str.length();
        if (len == 0) return 0;
        
        long long r = 0;
        int flag = 1;
        
        int i = 0;
        while (str[i] == ' ') {
            i++;
        }
        
        if (str[i]=='-') {
            flag = -1;
            i++;
        } else if (str[i]=='+'){
            i++;
        }
        for (; i < len; i++) {
            char ch = str[i];
            if (ch >= '0' && ch <= '9') {
                r = r * 10 + ch - '0';
            } else {
                break;
            }
            if (r > numeric_limits<int>::max()) break;
        }
        
        if ((flag == 1 && r > numeric_limits<int>::max())
            || (flag == -1 && -r < numeric_limits<int>::min())) {
            return flag == 1 ? numeric_limits<int>::max() : numeric_limits<int>::min();
        } else {
            return r * flag;
        }
    }


    int myAtoi_backup(string str) {
        if (str.length() == 0) return 0;
        long long r = 0;
        int flag = 1;
        
        int i = 0;
        if (str[0] == '-') {
            flag = -1;
            i++;
        } else if (str[0] == '+') {
            i++;
        }
        
        int base = 10;
        if (str[i] == '0' && i+1 < str.length() && (str[i+1] == 'x' || str[i+1] == 'X')) {
            base = 16;
            i += 2;
        } else if (str[i] == '0') {
            base = 8;
            i++;
        }
        
        for (; i < str.length(); i++) {
            char ch = str[i];
            if ((base == 10 && ch >= '0' && ch <= '9')
                || (base == 8 && ch >= '0' && ch <= '7')
                || (base == 16 && (ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'f' || ch >= 'A' && ch <= 'F'))) {
                r = r * base;
                int delta = ch - '0';
                if (base == 16) {
                    delta = 10;
                    if (ch >= 'a' && ch <= 'f') {
                        delta += ch - 'a';
                    } else if (ch >= 'A' && ch <= 'F') {
                        delta += ch - 'A';
                    }
                }
                r += delta;
                if (r > numeric_limits<int>::max()) {
                    return 0;
                }
            } else {
                return r * flag;
            }
        }
        return r * flag;
    }
};
    原文作者:knzeus
    原文地址: https://segmentfault.com/a/1190000007274998
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞