class Solution{ public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 比较版本号 * @param version1 string字符串 * @param version2 string字符串 * @return int整型 */ vector<string> token(string s,char c){ int len=0; s=s+c; vector<string> strings; for(int i=0;i<s.length();i++){ if(s[i]==c){ len++; } } string res[len+1]; int j=0; for(int i=0;i<s.length();i++){ if(s[i]!=c){ res[j]=res[j]+char(s[i]); }else{ strings.push_back(res[j]); j++; } } return strings; } string equallenth(string s,string l){ // if(s.size()==l.size()){ // } int zero=-(s.length()-l.length()); if(s.size()<l.size()){ for(int k=0;k<zero;k++){ s="0"+s; } } return s; } vector<string> equallenv(vector<string> &s,vector<string> &l){ int zero=-(s.size()-l.size()); for(int k=0;k<zero;k++){ s.push_back("0"); } return s; } int cmp(int i,vector<string> &strings1,vector<string> &strings2){ //int len=strings1.size(); int res=0; if(strings1[i].size()<strings2[i].size()){ strings1[i]=equallenth(strings1[i],strings2[i]); } if(strings1[i].size()>strings2[i].size()){ strings2[i]=equallenth(strings2[i],strings1[i]); } int len=strings1[i].size(); for(int j=0;j<len;j++){ if(strings1[i][j]<strings2[i][j]){ res=-1; break; } if(strings1[i][j]>strings2[i][j]){ res=1; break; } } return res; } int compare(string version1, string version2) { // write code here int res=0; vector<string> strings1=token(version1,'.'); vector<string> strings2=token(version2,'.'); if(strings1.size()<strings2.size()){ strings1=equallenv(strings1,strings2); } if(strings1.size()>strings2.size()){ strings2=equallenv(strings2,strings1); } for(int index=0;index<strings1.size();index++){ if(cmp(index,strings1,strings2)==-1){ res=-1; break; } if(cmp(index,strings1,strings2)==1){ res=1; break; } } return res; } };