写的蠢了点,但是也算是个思路吧,面试官
int compareVersion(string s1, int index1, string s2, int index2) {
//if s1 is larger, return 1, if s2 is larger return 2, else return 0
if (index1 < s1.size() && index2 < s2.size()) {
int tmpIndex1 = index1;
int prev1 = 0;
while (tmpIndex1 < s1.size()&&s1[tmpIndex1]!='.') {
int cur = (int)(s1[tmpIndex1] - '0');
prev1 = prev1*10 + cur;
tmpIndex1++;
}
int prev2 = 0;
int tmpIndex2 = index2;
while (tmpIndex2 < s2.size()&&s2[tmpIndex2]!='.') {
int cur = (int)(s2[tmpIndex2] - '0');
prev2 = prev2*10 + cur;
tmpIndex2++;
}
if (prev1 > prev2) return 1;
else if (prev1 < prev2) return 2;
else return compareVersion(s1, tmpIndex1+1, s2, tmpIndex2+1);
}
else if (index1 < s1.size()) {
int tmpIndex1 = index1;
int prev1 = 0;
while (tmpIndex1 < s1.size()&&s1[tmpIndex1]!='.') {
int cur = (int)(s1[tmpIndex1] - '0');
prev1 = prev1*10 + cur;
tmpIndex1++;
}
if (prev1>0) return 1;
else return 0;
}
else if (index2 < s2.size()) {
int tmpIndex2 = index2;
int prev2 = 0;
while (tmpIndex2 < s2.size()&&s2[tmpIndex2]!='.') {
int cur = (int)(s2[tmpIndex2] - '0');
prev2 = prev2*10 + cur;
tmpIndex2++;
}
if (prev2>0) return 2;
else return 0;
}
else return 0;
}