Compare two version numbers version1 and version2.
If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.
You may assume that the version strings are non-empty and contain only digits and the .
character.
The .
character does not represent a decimal point and is used to separate number sequences.
For instance, 2.5
is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
Here is an example of version numbers ordering:
0.1 < 1.1 < 1.2 < 13.37
class Solution { public: int strToInt(string& s,int start,int end) { int res = 0; while(start<end){ res = res*10+(s[start++]-‘0‘); } return res; } int compareVersion(string version1, string version2) { int v1size = version1.size(); int v2size = version2.size(); int i=0,j=0,starti=0,startj=0; int v1=0,v2=0; while(i<v1size && j<v2size){ while(i<v1size && version1[i]!=‘.‘) i++; while(j<v2size && version2[j]!=‘.‘) j++; v1 = i==0 ? -1 :strToInt(version1,starti,i); v2 = j==0 ? -1 :strToInt(version2,startj,j); if(v1<v2){ return -1; }else if(v1>v2){ return 1; }else{ starti = ++i; startj = ++j; } } while(i<v1size && (version1[i]==‘0‘ || version1[i]==‘.‘)) i++; while(j<v2size && (version2[j]==‘0‘ || version2[j]==‘.‘)) j++; if(i<v1size){ return 1; } if(j<v2size){ return -1; } return 0; } };
[string]Compare Version Numbers
原文:http://www.cnblogs.com/zengzy/p/5036507.html