Runtime error when str == ""


  • 0
    D

    I have provided a dealing method for the case str == "" , why there is still a runtime error ?

    int myAtoi(char* str) {
    int len = strlen(str);
    char digit0 = '0';
    char digit1 = '1';
    char digit9 = '9';
    int res = 0;
    int i = 0, flag = 1;
    if (str == "")
    	return 0;
    while (str[i]<digit0 || str[i]>digit9) {
    	i++;
    }
    if (str[i - 1] == '-') {
    	flag = -1;
    }
    else if (str[i - 1] == '+') {
    	flag = +1;
    }
    while (str[i] == digit0) {
    	i++;
    }
    while (str[i] >= digit1 && str[i] <= digit9) {
    	res = res * 10 + (str[i] - '0');
    	if (res > INT_MAX / 10) {
    		return (flag)*INT_MAX;
    	}
    	i++;
    }
    return flag*res;
    }

Log in to reply
 

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