Compare Version Numbers
1 typedef struct Version 2 { 3 vector<int> vec; 4 }Version; 5 int cmp(Version a,Version b) 6 { 7 int i; 8 int la=a.vec.size(),lb=b.vec.size(); 9 for(i=0;i<la&&i<lb;i++) 10 { 11 if(a.vec[i]!=b.vec[i]) 12 return a.vec[i]>b.vec[i]?1:-1; 13 } 14 if(la!=lb) 15 return la>lb?1:-1; 16 return 0; 17 } 18 class Solution { 19 public: 20 int compareVersion(string version1, string version2) { 21 Version v1=toDouble(version1); 22 Version v2=toDouble(version2); 23 return cmp(v1,v2); 24 } 25 Version toDouble(string v) 26 { 27 int n=v.size(),i; 28 int ans=0; 29 Version ver; 30 ver.vec.clear(); 31 for(i=0;i<n;i++) 32 { 33 if(isdigit(v[i])) 34 { 35 ans=ans*10+v[i]-'0'; 36 } 37 else 38 { 39 ver.vec.push_back(ans); 40 ans=0; 41 } 42 } 43 ver.vec.push_back(ans); 44 for(i=ver.vec.size()-1;i>0;i--) 45 { 46 if(ver.vec[i]==0) 47 ver.vec.pop_back(); 48 else 49 break; 50 } 51 return ver; 52 } 53 };
非常纠结的一道题,对于版本号 1.0>0.1 , 1.0=1, 0.0.1<0.1,
可以有多个point,但是不考虑末位0.
将字符串转化为整数stoi(str);