LeetCode 008 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.

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.

分析:

① 无效的格式,如:"+-1234", "+-c1234";

② 有效的格式,如:"-123c4", "+1234";

③ 数据溢出,如:"2147483648"。

代码如下:

class Solution {
public:

       int atoi(const char *str) {

        int i = 0, flag = 1;
        int num = 0;

        //忽略空字符
        for(; str[i] != ''; i++){
            if(str[i] == ' ') continue;
            else break;
        }
        
        //正号和负号只能有一个,多个无效
        if(str[i] == '+') i++;
        else if(str[i] == '-'){
            flag = -1;
            i++;
        }
        
        for (; i < strlen(str); i++) {
            if (str[i] < '0' || str[i] > '9') break;
            //防止数据溢出
            if (num > INT_MAX / 10 || (num == INT_MAX / 10 && (str[i] - '0') > INT_MAX % 10)) {
                return flag == -1 ? INT_MIN : INT_MAX;
            }
            num = num * 10 + str[i] - '0';
        }
        return num * flag;
        
    }
};

 Java:

    public static int myAtoi(String str) {

        final int INT_MAX = 2147483647;
        final int INT_MIN = -2147483648;

        int i = 0, flag = 1, num = 0;

        //先忽略掉前面的空格
        for (; i < str.length() && str.charAt(i) != ''; i++) {
            if (str.charAt(i) == ' ') {
                continue;
            } else {
                break;
            }
        }

        //判断第一个符号位,只能出现一个
        if (i < str.length() && str.charAt(i) == '+') {
            i++;
        } else if (i < str.length() && str.charAt(i) == '-') {
            flag = -1;
            i++;
        }

        for (; i < str.length(); i++) {

            if (str.charAt(i) > '9' || str.charAt(i) < '0') {
                break;
            }

            //分三种情况,防止溢出~
            if (num > INT_MAX
                    || num > INT_MAX / 10
                    || (num == INT_MAX / 10 && ((str.charAt(i) - '0') > INT_MAX % 10))) {
                return (flag == 1) ? INT_MAX : INT_MIN;
            }

            num = num * 10 + str.charAt(i) - '0';
            System.out.println(num);
        }

        return num * flag;
    }
原文地址:https://www.cnblogs.com/510602159-Yano/p/4278677.html