My C++ recursive solution


  • 0
    L
    class Solution {
    public:
        int compareVersion(string version1, string version2) 
        {
            int v1 = (int)stof(version1);
            int v2 = (int)stof(version2);
            if (v1 < v2) return -1;
            else if (v1 > v2) return 1;
            else
            {
                int p1 = 0, p2 = 0;
                while (p1 < version1.size() && version1[p1] != '.') ++p1;
                while (p2 < version2.size() && version2[p2] != '.') ++p2;
                if (p1 == version1.size() && p2 == version2.size()) return 0;
                else if (p1 == version1.size() && p2 != version2.size())
                    return compareVersion(to_string(0), version2.substr(p2 + 1, version2.size() - p2));
                else if (p1 != version1.size() && p2 == version2.size())
                    return compareVersion(version1.substr(p1 + 1, version1.size() - p1), to_string(0));
                else
                    return compareVersion(version1.substr(p1 + 1, version1.size() - p1), version2.substr(p2 + 1, version2.size() - p2));
            }
        }
    };
    

Log in to reply
 

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