8ms ugly c++ solution


  • 0
    Z

    A bit more parsing. A bit more ugly ... but 8ms....... pretty ain't everything...

    class Solution {
    public:
    
        int myAtoi(string str)
        {
            int len = str.length();
            int fdx = -1;
            int idx = 0;
            int seenSign = 0; // -1 neg 0 nope 1 pos
            while (idx < len)
            {
                char ch = str[idx];
                if (ch == '+')
                {
                    if (seenSign != 0)
                    {
                        return 0;
                    }
                    seenSign = 1;
                }
                else if (ch == '-')
                {
                    if (seenSign != 0)
                    {
                        return 0;
                    }
                    seenSign = -1;
                }
                else
                if (isspace(ch) || !isdigit(ch))
                {
                    if (!isspace(ch) && !isdigit(ch) && fdx == -1)
                    {
                        return 0;
                    }
                    if ((seenSign != 0) && (fdx == -1))
                    {
                        return 0;
                    }
                    if (fdx != -1)
                    {
                        idx--;
                        break;
                    }
                }
                else if (isdigit(ch))
                    {
                        if (fdx == -1)
                        {
                            fdx = idx;
                        }
                    }
                    else
                    {
                        break;
                    }
                
                idx++;
            }
            int result = 0;
            
            if (fdx >= 0)
            {
                if (idx == len)
                {
                    idx--;
                }
                int validLen = (idx - fdx);
                //INT_MIN (-2147483648)
                //INT_MAX ( 2147483647)
                if (validLen > 10)
                {
                    if (seenSign == -1)
                    {
                        result =  INT_MIN;
                    }
                    else
                    {
                        result = INT_MAX;
                    }
                }
                else
                {
                    int over = 0;
                    if (validLen == 10)
                    {
                        int digit = str[fdx] - 'a';
                        if (digit > 1)
                        {
                            over = 1;
                        }
                    }
                    int factor = 1;
                    while (idx >= fdx)
                    {
                        char ch = str[idx];
                        int digit = (ch-'0');
                        if ((seenSign == -1) &&
                            (result >= 147483648) &&
                            (digit >= 2))
                        {
                            over = 2;
                        }
                        else if ((seenSign >= 0) &&
                                 (result >= 147483647) &&
                                 (digit >= 2))
                        {
                            over = 1;
                        }
                        result += (digit * factor);
                        if ((factor > 1) && (result < 0 || (factor%10 != 0)))
                        {
                            over = 1;
                            break;
                        }
                        factor *= 10;
                        idx--;
                    }
                    if (over > 0)
                    {
                        if (seenSign == -1)
                        {
                            result = INT_MIN;
                        }
                        else
                        {
                            result = INT_MAX;
                        }
                    }
                    if (seenSign == -1)
                    {
                        result *= -1;
                    }
                }
            }
            return result;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.