C++ solution compare version


  • 0
    Y
    int compareVersion(string version1, string version2) {
            stringstream ss1(version1), ss2(version2);
            bool eof1 = false, eof2 = false;
            while (!(eof1 && eof2)) {
                int num1 = 0, num2 = 0;
                string buf1, buf2;
                if (getline(ss1, buf1, '.')) {
                    num1 = stoi(buf1);
                } else {
                    eof1 = true;
                }
                if (getline(ss2, buf2, '.')) {
                    num2 = stoi(buf2);
                } else {
                    eof2 = true;
                }
                if (num1 > num2) {
                    return 1;
                } else 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.