题意:比较版本号的大小
有点变态,容易犯错
本质是字符串的比较,请注意他的版本号的小数点不知1个,有的会出现01.0.01这样的变态版本号
class Solution {
public:
int cmp_num(string a, string b){
if(a.size() < b.size()) return -;
else if(a.size() == b.size()){
if(a == b) return ;
else return a < b ? -: ;
}
else return ;
}
vector<string> change(string v){
vector<string> vs;
int a = , b = ;
while((b = v.find(".",a)) != string::npos){
string::size_type i = a;
for(; i < v.size(); ++i){
if(v[i] > '' || v[i] == '.') break;
}
if(i == v.size() || v[i] == '.') vs.push_back("");
else vs.push_back(v.substr(i, b- i));
a = b + ;
}
string::size_type i = a;
for(; i < v.size(); ++i){
if(v[i] > '' || v[i] == '.') break;
}
if(i == v.size() || v[i] == '.') vs.push_back("");
else vs.push_back(v.substr(i, v.size() - i));
return vs;
}
int compareVersion(string version1, string version2) {
vector<string> v1,v2;
v1 = change(version1);
v2 = change(version2); string::size_type i;
for(i = ; i < v1.size() && i <v2.size(); ++i){
if(cmp_num(v1[i],v2[i]) != ) return cmp_num(v1[i],v2[i]);
} for(; i < v1.size(); ++i){
if(cmp_num(v1[i],"") != ) return cmp_num(v1[i],"");
} for(; i < v2.size(); ++i){
if(cmp_num("",v2[i]) != ) return cmp_num("",v2[i]);
} return ;
}
};