细节题,代码如下:
int compareVersion(string version1, string version2) {
int index1 = 0, index2 = 0;
while(index1 < version1.length() || index2 < version2.length()){
int temp1, temp2;
int end1 = version1.find('.', index1);
int end2 = version2.find('.', index2);
if(index1 == string::npos)
temp1 = 0;
else if(end1 != string::npos)
temp1 = atoi(version1.substr(index1, end1 - index1 + 1).c_str());
else
temp1 = atoi(version1.substr(index1).c_str());
if(index2 == string::npos)
temp2 = 0;
else if(end2 != string::npos)
temp2 = atoi(version2.substr(index2, end2 - index2 + 1).c_str());
else
temp2 = atoi(version2.substr(index2).c_str());
if(temp1 == temp2){
index1 = end1 == string::npos ? end1 : end1 + 1;
index2 = end2 == string::npos ? end2 : end2 + 1;
}
else
return temp1 > temp2 ? 1:-1;
}
return 0;
}