4ms C solution, atoi


  • 0
    W
    int myAtoi(char* str) {
    long long result = 0L;
    int sign = 1;
    while( isspace(*str) )str++;
    if( isalpha(*str) ) return 0;
    if( isdigit(*(str+1)) ){
        if( isdigit(*(str)) ){
            result = result * 10 + *str++ - '0';
        }
        else{
        	if( '-' == *str ){
    	      sign = -1;
        	}
        	else if( '+' == *str ){
        	    sign = sign;
        	}
    	    str++;
        }
    }
    if( isdigit(*(str)) ){
    	while( isdigit(*str) ){
        	result = result * 10 + *str++ - '0';
        	if( sign * result > INT_MAX ) return INT_MAX;
            else if( sign * result < -INT_MAX ) return -INT_MAX-1;
    	}
    }
    return sign * result;
    

    }


Log in to reply
 

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