My C-language 4ms solution


  • 1
    T
    int myAtoi(char* str) {
            bool flag = true;
            long int r = 0;
            while(*str == ' ' || *str == '\t' || *str == '\n') ++str;
            if(*str == '-') flag = false, ++str;
            else if(*str == '+') ++str;
            if(*str < '0' || *str > '9') return 0;
            int i = 0;
            while(*str != '\0')
            {
                if(*str >= '0' && *str <= '9')
                {
                    r *= 10;
                    r += (long)(*str - '0');
                    ++i;
                }else if(i != 0) break;
                ++str;
                if(i > 10)
                    if(flag) return INT_MAX;
                    else return INT_MIN;
            }
            if(i == 0) return 0;
            if(flag)
            {
                if(r > INT_MAX) return INT_MAX;
                else return (int)(r);
            }else if((r *= -1) < INT_MIN) return INT_MIN;
            else return (int)(r);
        }

Log in to reply
 

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