It seem that have a bug when run this code.


  • 0
    D

    I have my solution as bellow. And I get a Wrong Answer as bellow. But when run the code myself, it will get the right answer. So I want to know is there any bug about my code.

    input:   "1.1"   "1.10"
    output:   0
    expected: -1
    
       int compareVersion(string version1, string version2) {
            vector<int> v1 = trans(version1);
            vector<int> v2 = trans(version2);
            int ind;
            for(; ind < v1.size() && ind < v2.size(); ind++) {
                if(v1[ind] < v2[ind]) return -1;
                if(v1[ind] > v2[ind]) return  1;
            }
            for(; ind < v1.size(); ind++) if(v1[ind] > 0) return  1;
            for(; ind < v2.size(); ind++) if(v2[ind] > 0) return -1;
            return 0;
        }
        
        vector<int> trans(string version) {
            istringstream in(version);
            vector<int> res;
            string num;
            while(getline(in, num, '.'))
                res.push_back(stoi(num));
            return res;
        }
    

Log in to reply
 

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