Clean & fast C++ solution 0ms


  • 0
    H
    int compareVersion(string version1, string version2) {
            int i = 0, j = 0;
            int n1, n2;
            
            while (i < version1.length() || j < version2.length()) {
                n1 = i < version1.length() ? atoi(version1.c_str() + i) : 0;
                n2 = j < version2.length() ? atoi(version2.c_str() + j) : 0;
                
                if (n1 < n2)
                    return -1;
                if (n1 > n2)
                    return 1;
                
                while (i < version1.length() && version1[i] != '.')
                    i++;
                while (j < version2.length() && version2[j] != '.')
                    j++;
                i++;
                j++;
            }
            
            return 0;
        }
    

Log in to reply
 

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