Help with this wield problem


  • 0
    H

    Can anybody help figuring out what is the problem with the following code?

     class Solution {
        public:
            int compareVersion(string version1, string version2) {
                int pos1, pos2;
                int ret;
                pos1 = version1.find_first_of('.');
                pos2 = version2.find_first_of('.');
                
                if(pos1 == string::npos && pos2 == string::npos)
                    return comparestr(version1,version2);
                if(pos1 == string::npos){        // pos2 != string::npos
                    ret = comparestr(version1,version2.substr(0,pos2));
                    if(ret != 0)
                        return ret;
                    pos2++;
                    while(pos2 < version2.length()){
                        if(version2[pos2++] != '0')
                            return -1;
                    }
                    return 0;
                }
                if(pos2 == string::npos){
                    ret = comparestr(version1.substr(0,pos1),version2);
                    if(ret != 0)
                        return ret;
                    pos1++;
                    while(pos1 < version1.length()){
                        if(version1[pos1++] != '0')
                        return 1;
                    }
                    return 0;
                }
                ret = comparestr(version1.substr(0,pos1),version2.substr(0,pos2));
                if(ret != 0)
                    return ret;
                return compareVersion(version1.substr(pos1+1,version2.substr(pos2+1)),;
            }
            
            int comparestr(const string str1, const string str2){
                int num1 = stoi(str1);
                int num2 = stoi(str2);
                if(num1 > num2)
                    return 1;
                if(num1 < num2)
                    return -1;
                return 0;
            }
            
        };

Log in to reply
 

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