class Solution {
public:
int compareVersion(string version1, string version2) {
int sum1 = 0,sum2 = 0;
int len1 = version1.size(),len2 = version2.size();
int index1 = 0,index2 = 0;
// while(index1 < len1 && index2 < len2){
//这样 1.0.1 和 1 会挂掉
while(index1 < len1 || index2 < len2){
sum1 = 0;
sum2 = 0;
while(index1 < len1 && version1[index1] != '.'){
sum1 = sum1 * 10 + (version1[index1]-'0');
++index1;
}
while(index2 < len2 && version2[index2] != '.'){
sum2 = sum2 * 10 + (version2[index2]-'0');
++index2;
}
index1++;
index2++;
if(sum1 > sum2) return 1;
if(sum1 < sum2) return -1;
}
return 0;
}
};
//每次遇到一个冒号就要将冒号前面的数字拎出来。
//然后再比较冒号之间间隔的数字是多大
//上面是正确的思路
2022-1-15 Leetcode 165.比较版本号
最新推荐文章于 2024-09-16 13:04:24 发布