Untitled

 avatar
unknown
plain_text
a year ago
1.3 kB
5
Indexable
class Solution {
public:
    int compareVersion(string x, string y) {
        int n =x.length();
        int m =y.length();
        vector<int> first, second;
        string cur="";
        for(int i=0;i<n;i++) {
            if(x[i] == '.') {
                first.push_back(stoi(cur));
                cur="";
            }
            else{
                cur.push_back(x[i]);
            }
        }
        first.push_back(stoi(cur));
        
        string cur2="";
        for(int i=0;i<m;i++) {
            if(y[i] == '.') {
                second.push_back(stoi(cur2));
                cur2="";
            }
            else{
                cur2.push_back(y[i]);
            }
        }
        second.push_back(stoi(cur2));

        int i = 0;
        int j = 0;
        while(i<first.size() && j<second.size()) {
            if(first[i] < second[j])
                return -1;
            else if (first[i] > second[j])
                return 1;
            i++;
            j++;
        }
        while(i<first.size()) {
            if(first[i]!=0)
                return 1;
            i++;
        }
        while(j<second.size()) {
            if(second[j]!=0)
                return -1;
            j++;
        }
        return 0;
    }
};

// 1.21090.1.4 -> 1, 2, 1, 4
// 1.020.1 -> 1, , 1, 0 

// 1.2 -> 1, 2
// 1.10 -> 1, 10
Editor is loading...
Leave a Comment