0ms recursive solution


  • 0
    B
    class Solution {
    public:
        int compareVersion(string version1, string version2) {
            if(version1 ==  version2) return 0;
            size_t idx1 = version1.find('.');
            size_t idx2 = version2.find('.');
            string part1 = version1.substr(0, idx1);
            string part2 = version2.substr(0, idx2);
            if(part1 != part2) return compare(part1,part2);
            part1 = "0";
            if(idx1 != string::npos) part1 = version1.substr(idx1+1);
            part2 = "0";
            if(idx2 != string::npos) part2 = version2.substr(idx2+1);
            return compareVersion(part1, part2);
        }
        int compare(string str1, string str2){
            int i1 = stoi(str1), i2 = stoi(str2);
            if(i1 == i2) return 0;
            if(i1 > i2) return 1;
            return -1;
        }
    };

Log in to reply
 

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