Awesome superfast solution in C (faster than 100% c solutions)


  • -2
    H

    int compareVersion(char* version1, char* version2) {
    char *ver1 = NULL;
    char *ver2 = NULL;
    int val1=0,val2=0;

    ver1 = version1;
    ver2 = version2;
    
    while((*ver1 != '\0')||(*ver2 != '\0')) {
        while((*ver1 != '\0') && (*ver1 != '.')) {
            val1 = (val1*10)+(*ver1-'0');
            ver1++;
        }
        while( (*ver2 != '\0') && (*ver2 != '.')) {
            val2 = (val2*10)+(*ver2-'0');
            ver2++;
        }
        if (val1 > val2) return 1;
        else if (val2 > val1) return -1;
        val1=val2=0;
        if (*ver1 =='.') ver1++;
        if (*ver2 =='.') ver2++;
        
    }
    return 0;
    

    }


Log in to reply
 

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