C++ with atoi()


  • 0
    D
    public:
        int compareVersion(string version1, string version2) {
            vector<int> v1,v2; string tmp=""; 
            for(auto i : version1){
                if(i != '.') tmp += i;
                else {v1.push_back(atoi(tmp.c_str())); tmp = "";}
            }
            v1.push_back(atoi(tmp.c_str())); tmp = "";
            
            for(auto i : version2){
                if(i != '.') tmp += i;
                else {v2.push_back(atoi(tmp.c_str())); tmp = "";}
            }
            v2.push_back(atoi(tmp.c_str())); int i = 0;
            int s1 = v1.size(), s2 = v2.size();
            while(i < s1 && i < s2){
                if(v1[i] > v2[i]) return 1;
                else if(v1[i] < v2[i]) return -1;
                i++;        
            }
            if(s1 > s2)
                for(int j = i; j < s1; ++j)
                if(v1[j] != 0) return 1;
            if(s1 < s2)
                for(int j = i; j < s2; ++j)
                if(v2[j] != 0) return -1;
            return 0;
        }
    };

Log in to reply
 

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