cpp
class Solution {
public:
stack<int> s1;
stack<int> s2;
long long getvalue(string s,int i,bool flag)
{
long long ret = 0;
while (i < s.size() && s[i] == '0')
{
i++;
if (flag)
s1.push(i);
else s2.push(i);
}
while (i<s.size()&& s[i] != '.')
{
ret = ret * 10 + s[i] - '0';
i++;
if (flag)
s1.push(i);
else s2.push(i);
}
return ret;
}
int compareVersion(string version1, string version2)
{
s1.push(-1);
s2.push(-1);
while (s1.top()+1 < version1.size() && s2.top()+1 < version2.size())
{
int ret = getvalue(version1, s1.top() + 1, 1) - getvalue(version2, s2.top() + 1, 0);
if (ret > 0) return 1;
else if (ret < 0) return -1;
}
int ret1 = 0;
int ret2 = 0;
while (s1.top() + 1 < version1.size())
{
ret1 += getvalue(version1, s1.top() + 1, 1);
}
while (s2.top() + 1 < version2.size())
{
ret2 += getvalue(version2, s2.top() + 1, 0);
}
if (ret1 > ret2)
return 1;
else if (ret1 < ret2)
return -1;
return 0;
}
};
本人写的代码,不一定是最优解