4ms solution in c


  • 1
    B

    int myAtoi(char* str) {

    int len = strlen(str);
    int i = 0;
    const int MAX_POS = 2147483647;
    const int MAX_NEG = -2147483648;
    int sign = 1;
    int max = MAX_POS;
    int rval = 0;
    
    while (str[i] == ' ' && i<len)
        i++;
        
    if (str[i] == '-') {
        sign = -1;
        max = MAX_NEG;
    }
        
    if (str[i] == '-' || str[i] == '+')
        i++;
    
    for (; i<len; i++) {
        if (str[i] < '0' || str[i] > '9')
            break;
        
        if (((max - (str[i] - '0')*sign) / 10)*sign >= rval)
            rval = rval*10 + str[i] - '0';
        else
            return max;
    }
    
    return sign*rval;
    

    }


Log in to reply
 

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