Clear 0ms C++ solution


  • 0
    G
    class Solution {
    public:
        int compareVersion(string version1, string version2)
        {
            if(version1 == version2)
            {
                return 0;
            }
            else
            {
                int v1 = 0, v2 = 0;
                while(v1 < version1.length() || v2 < version2.length())
                {
                    string temp1 = "";
                    string temp2 = "";
                    while(version1[v1] != '.' && v1 < version1.length())
                    {
                        temp1 +=version1[v1];
                        ++v1;
                    }
                    
                    while(version2[v2] != '.' && v2 < version2.length())
                    {
                        temp2 +=version2[v2];
                        ++v2;
                    }
                    
                    if(temp1 == "") temp1 = "0";
                    if(temp2 == "") temp2 = "0";
                    
                    if(stoi(temp1) != stoi(temp2))
                    {
                        return stoi(temp1) > stoi(temp2) ? 1 : -1;
                    }
                    ++v1;
                    ++v2;
                }
                return 0;
            }
        }
    };

Log in to reply
 

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