My 0ms clear C++ solution


  • 0
    T
    int compareVersion(string version1, string version2) 
    {
        int v1Walker = 0;
        int v2Walker = 0;
        int size1 = version1.size();
        int size2 = version2.size();
        int v1Num = 0;
        int v2Num = 0;
        
        
        while(v1Walker != size1 ||  v2Walker != size2)
        {
            if(v1Walker != size1 && version1[v1Walker] != '.')
            {
                v1Num = v1Num * 10 + version1[v1Walker] - '0';
                v1Walker = min(size1, v1Walker + 1);
            }
            
            if(v2Walker != size2 && version2[v2Walker] != '.')
            {
                v2Num = v2Num * 10 + version2[v2Walker] - '0';
                v2Walker = min(size2, v2Walker + 1);
            }
            
            if((v1Walker == size1 || version1[v1Walker] == '.') && (v2Walker == size2 || version2[v2Walker] == '.'))
            {
                if(v1Num > v2Num)
                {
                    return 1;
                }
                else if(v1Num < v2Num)
                {
                    return -1;
                }
                
                v1Num = 0;
                v2Num = 0;
                
                v1Walker = min(size1, v1Walker + 1);
                v2Walker = min(size2, v2Walker + 1);
            }
        }
        
        return 0;
    }
    

    The idea is to line up the first and second version numbers before you go ahead.


  • 1
    M

    A similar answer, but slightly more concise.

    class Solution {
    public:
        int compareVersion(string version1, string version2) {
            int j = 0;
            int i = 0;
            while(i <= version1.size() || j <= version2.size()){
                int v2 = 0;
                int v1 = 0;
                if(i <= version1.size()){
                    while('.' != version1[i] && '\0' != version1[i]){
                        v1 *= 10;
                        v1 += version1[i] - '0';
                        i++;
                    }
                }
                
                if(j <= version2.size()){
                    while('.' != version2[j] && '\0' != version2[j]){
                        v2 *= 10;
                        v2 += version2[j] - '0';
                        j++;
                    }
                }
                if(v1 > v2) return 1;
                if(v1 < v2) return -1;
                i++; j++;
            }
            
            return 0;
        }
    };

  • 0
    B

    excellent one


  • 0
    I

    mine is similar too...

    class Solution {
    public:
        int compareVersion(string version1, string version2) {
            int l1 = version1.size();
            int l2 = version2.size();
            int i1 = 0, i2 = 0, v1 = 0, v2 = 0;
            while((i1<l1)|(i2<l2)){
                while((i1<l1)&(version1[i1]!='.')){
                    v1 = 10*v1+(version1[i1]-'0');
                    i1++;
                }
                while((i2<l2)&(version2[i2]!='.')){
                    v2 = 10*v2+(version2[i2]-'0');
                    i2++;
                }
                if(v1>v2)
                    return 1;
                if(v2>v1)
                    return -1;
                i1++;
                i2++;
                v1 = 0;
                v2 = 0;
            }
            return 0;
        }
    };

Log in to reply
 

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