leetcode programming record 1#8String to Integer (atoi)

Keywords: IE ascii

Today is the first time to record the problem-solving process on leetcode.
Let's start with the first question:

  1. 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.

Problem-solving analysis:
First of all, we need to understand that the title requires us to implement a function that can convert the number in ASCII code to the number. This function is the atoi function commonly used in the c library function. This function converts strings into integer numbers has four points to pay attention to:

  1. The function automatically ignores the spaces and tabs at the beginning of the string until it encounters positive and negative signs or numbers.
  2. Conversion begins when a sign or a number is encountered, and then ends when a non-numeric character is encountered.
  3. If the string does not start with a space character, a tab, a sign, or a number, the return value of the function is 0.
  4. If the number represented by the string exceeds the range of int data, INT_MAX is returned when the number overflows and INT_MIN is returned when the number overflows.

    According to these four requirements, you can start processing strings. The following is my c++ code. Because I didn't consider some situations at first, the code that was modified later was a little bloated.

#include <string>
#include <limits.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
using namespace std;

class Solution
{
public:
    int myAtoi(string str)
    {
        int result = 0;
        string sresult;
        for(int i = 0; i < str.size(); i++)
        {
            if((str[i] == ' ' || str[i] == '\t') && sresult.size() == 0)
            {
                continue;
            }
            else
            {
                if ((str[i] == '+' || str[i] == '-') && sresult.size() == 0)
                {
                    sresult.push_back(str[i]);
                }
                else if(str[i] <= '9' && str[i] >= '0')
                {
                    sresult.push_back(str[i]);
                }
                else
                {
                    break;
                }
            }
        }
        const int bitsOfInt = ((int)log10(INT_MAX)) + 1;

        if(sresult.size() == 0)
        {
            return 0;
        }
        else if(sresult.size() == 1)
        {
            if(sresult[0] == '+' || sresult[0] == '-')
            {
                return 0;
            }
            else
            {
                return sresult[0] - '0';
            }
        }
        else
        {
            if(sresult[0] == '+' )
            {
                if(sresult.size() > bitsOfInt + 1)
                {
                    return INT_MAX;
                }
                for (int i = 1; i < sresult.size(); i++)
                {

                    result = result * 10 + sresult[i] - '0';
                    if(result > INT_MAX / 10 && sresult.size() == bitsOfInt + 1 && i == sresult.size() - 2)
                    {
                        return INT_MAX;
                    }
                    if(result == INT_MAX / 10 && sresult.size() == bitsOfInt + 1)
                    {
                        if(sresult[i + 1] - '0' > INT_MAX - INT_MAX / 10 * 10)
                        {
                            return INT_MAX;
                        }
                    }

                }
            }
            else if(sresult[0] == '-')
            {
                if(sresult.size() > bitsOfInt + 1)
                {
                    return INT_MIN;
                }
                for (int i = 1; i < sresult.size(); i++)
                {
                    result = result * 10 - sresult[i] + '0';
                    if(result < INT_MIN / 10 && sresult.size() == bitsOfInt + 1 && i == sresult.size() - 2)
                    {
                        return INT_MIN;
                    }
                    if(result == INT_MIN / 10 && sresult.size() == bitsOfInt + 1)
                    {
                        if('0' - sresult[i + 1] < INT_MIN - INT_MIN / 10 * 10)
                        {
                            return INT_MIN;
                        }
                    }
                }
            }
            else
            {
                if(sresult.size() > bitsOfInt)
                {
                    return INT_MAX;
                }
                for (int i = 0; i < sresult.size(); i++)
                {
                    result = result * 10 + sresult[i] - '0';
                    if(result > INT_MAX / 10 && sresult.size() == bitsOfInt && i == sresult.size() - 2)
                    {
                        return INT_MAX;
                    }
                    if(result == INT_MAX / 10 && sresult.size() == bitsOfInt)
                    {
                        if(sresult[i + 1] - '0' > INT_MAX - INT_MAX / 10 * 10)
                        {
                            return INT_MAX;
                        }
                    }
                }
            }
        }
        return result;
    }
};

Posted by razz on Wed, 13 Feb 2019 12:18:19 -0800