题解 | #比较版本号#
比较版本号
http://www.nowcoder.com/practice/2b317e02f14247a49ffdbdba315459e7
字符串
简单字符串处理
C++代码:
class Solution {
public:
int compare(string version1, string version2) {
int i = 0, j = 0, len1 = version1.size(), len2 = version2.size();
while (true) {
long long pa = 0, pb = 0;
while (i < len1) {
if (version1[i] == '.') {
i++;
break;
} else {
pa = pa * 10 + version1[i] - '0';
}
i++;
}
while (j < len2) {
if (version2[j] == '.') {
j++;
break;
} else {
pb = pb * 10 + version2[j] - '0';
}
j++;
}
if (pa > pb) {return 1;}
if (pa < pb) {return -1;}
if (i == len1 && j == len2) {return 0;}
}
}
};