Solution using C string functions


  • 0
    A
    int compareVersion(char* version1, char* version2) {
        char *token = NULL;
        int ver1Int, ver2Int;
        const char *search = ".";
        
        while (version1 != NULL || version2 != NULL) {
            ver1Int = 0;
            ver2Int = 0;
            token = strtok(version1, search);
            if (token != NULL) {
                ver1Int = atoi(token);
                version1 = strtok(NULL, "\0");
            }
            token = strtok(version2, search);
            if (token != NULL) {
                ver2Int = atoi(token);
                version2 = strtok(NULL, "\0");
            }
            
            if (ver1Int < ver2Int) {
                return -1;
            } 
            
            if (ver1Int > ver2Int) {
                return 1;
            } 
        } 
        
        return 0;
    }
    

Log in to reply
 

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