Share my super nasty C++ solution


  • 1
    Y
    #include <vector>
    using namespace std;
    class Solution {
    public:
        int compareVersion(string version1, string version2) {
            vector<string> v1;
            vector<string> v2;
            int i1 = 0;
            int i2 = 0;   //count the i in the version string
            string temp = "";
            while(i1 != version1.length()){
                if(version1[i1] != '.'){
                    temp += version1[i1];
                }
                else{
                    v1.push_back(temp);
                    temp = "";
                }
                ++i1;
            }
            v1.push_back(temp);
            temp = "";
            while(i2 != version2.length()){
                if(version2[i2] != '.'){
                    temp += version2[i2];
                }
                else{
                    v2.push_back(temp);
                    temp = "";
                }
                ++i2;
            }
            v2.push_back(temp);
            int j = 0;
            while(j < v1.size() && j < v2.size()){
                if(atoi(v1[j].c_str()) > atoi(v2[j].c_str())){
                    return 1;
                }
                else if(atoi(v1[j].c_str()) < atoi(v2[j].c_str())){
                    return -1;
                }
                else ++j;
            }
            if(v1.size() == v2.size()) return 0;
            else if (v1.size() < v2.size()){
                while(j < v2.size()){
                    if(atoi(v2[j].c_str()) != 0) return -1;
                    ++j;
                }
                return 0;
            }
            else {
                while(j < v1.size()){
                    if(atoi(v1[j].c_str()) != 0) return 1;
                    ++j;
                }
                return 0;
            }
        }
    };

  • 0
    Y

    why so diao....


  • 0
    Y

    Are you one of my friends?


Log in to reply
 

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