Leetcode integer of string conversion (atoi)

Title Description

Atoi you to implement a function, it can convert a string to an integer.

First, the function will begin with a space character discard useless if necessary, until the find to the first non-space character so far.

When we find the first non-space character is a positive or negative number, the combination of the symbols as much as possible with consecutive numbers up later, as the sign of integer; if the first non-space character is figures, which directly after the continuous numeric characters are combined to form an integer.

In addition to the string after a valid integer part may also exist extra characters, these characters can be ignored, they should not affect a function.

Note: if the character string in the first non-space character is not a valid integer character string is empty or contains only white space character string, then you will not need to be a function of conversion.

In any case, if the function can not effectively convert, 0 is returned.

Thinking

First skip space to find the first non-letter space, negative records, if it is '+' or '-' is added to the original position to continue a cycle generation consecutive number, out of the detected non-numeric, detecting whether the intermediate loop bounds

Code

method one:

class Solution {
public:
    int myAtoi(string str) {
        int len = str.length();
        if (len == 0)
            return 0;
        int start = 0;
        bool positive = true;
        while (str[start] == ' ')
        {
            start++;
            if (start == len)
                return 0;
        }
        if (str[start] == '-')
        {
            positive = false;
            start++;
        }
        else if (str[start] == '+')
        {
            start++;
        }
        long long res = 0;
        for (int i = start; i<len; i++)
        {
            if (str[i] >= '0' && str[i] <= '9')
            {
                res = res * 10 + str[i] - '0';
            }
            else
                break;
            if (res - 1 >= INT_MAX)
            {
                if (positive == false)
                    return INT_MIN;
                else
                    return INT_MAX;
            }
            
        }

        if (positive == false)
            return -res;
        else
            return res;
    }
};

Method Two:

class Solution {
public:
    int myAtoi(string str) {
        unsigned long len = str.length();

        // 去除前导空格
        int index = 0;
        while (index < len) {
            if (str[index] != ' ') {
                break;
            }
            index++;
        }

        if (index == len) {
            return 0;
        }

        int sign = 1;
        // 处理第 1 个非空字符为正负符号,这两个判断需要写在一起
        if (str[index] == '+') {
            index++;
        } else if (str[index] == '-') {
            sign = -1;
            index++;
        }

        // 根据题目限制,只能使用 int 类型
        int res = 0;
        while (index < len) {
            char curChar = str[index];
            if (curChar < '0' || curChar > '9') {
                break;
            }

            if (res > INT_MAX / 10 || (res == INT_MAX / 10 && (curChar - '0') > INT_MAX % 10)) {
                return INT_MAX;
            }
            if (res < INT_MIN / 10 || (res == INT_MIN / 10 && (curChar - '0') > -(INT_MIN % 10))) {
                return INT_MIN;
            }

            res = res * 10 + sign * (curChar - '0');
            index++;
        }
        return res;
    }
};
Published 85 original articles · won praise 0 · Views 375

Guess you like

Origin blog.csdn.net/weixin_38312163/article/details/105040233