class Solution {
public:
    int compareVersion(string version1, string version2) {
        istringstream v1(version1);
        istringstream v2(version2);

        while(!v1.eof() || !v2.eof())
        {
            string s1 = "0",s2 = "0";
            if(!v1.eof())
                getline(v1,s1,'.');

            if(!v2.eof())
                getline(v2,s2,'.');

            int n1 = atoi(s1.c_str());
            int n2 = atoi(s2.c_str());
            if(n1 == n2) continue;
            
            return (n1 > n2) ? 1 : -1;
        }
        return 0;
    }
};

运行效率:

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Compare Version Numbers.
Memory Usage: 6.3 MB, less than 39.19% of C++ online submissions for Compare Version Numbers.

使用istringstream能够简化整个程序,如果采用整体先过滤掉先导0再判断会导致判断逻辑相对复杂。
如果使用istringstream配合getline,s1 s2初始值设置为"0",每个小版本先比较会简化整个逻辑。