What's wrong with my cpp solution? Please take a look at.


  • 0
    G
    class Solution {
    public:
    int compareVersion(string version1, string version2) {
    //0.1 01.1 0 1.0
    int start1 = 0, end1 = version1.size() - 1;
    int start2 = 0, end2 = version2.size() - 1;
    
    while (start1<version1.size() && version1[start1] == '0') start1++;
    if (version1[start1] == '.' || start1 == version1.size()) start1--;
    while (end1 >= 0 && version1[end1] == '0') end1--;
    if (version1[end1] == '.') end1--;
    if (end1<0) end1++;
    
    while (start2<version2.size() && version2[start2] == '0') start2++;
    if (version2[start2] == '.' || start2 == version2.size()) start2--;
    while (end2 >= 0 && version2[end2] == '0') end2--;
    if (version2[end2] == '.') end2--;
    if (end2<0) end2++;
    
    string res1=version1.substr(start1, end1 - start1 + 1);
    string res2=version2.substr(start2, end2 - start2 + 1);
    
    return strcmp(res1.c_str(), res2.c_str());
    }
    
    };

Log in to reply
 

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