C++ easy to understand solution with explaination


  • 1
    X

    class Solution {

    public:

    tuple<int, string> helper(string v0){
        int int0;
        string next0;
        if(!v0.size()){
            int0=0;
            next0="";
        }
        else if(v0.find('.')!=string::npos){
            int0=stoi(v0.substr(0,v0.find('.')));
            next0=v0.substr(v0.find('.')+1);
        }
        else{
            int0=stoi(v0);
            next0="";
        }
        return make_tuple(int0, next0);
    }   
    int compareVersion(string v1, string v2) {
        int int1, int2;
        string next1, next2;
        
        tie(int1, next1) = helper(v1);
        tie(int2, next2) = helper(v2);
        
        if(int1>int2) return 1;
        if(int2>int1) return -1;
        if(int1==int2&&next1==""&&next2=="") return 0; 
        else return compareVersion(next1,next2);
    }
    

    };


Log in to reply
 

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