Compare numbers version1 and version2.
If version1 > version2 return 1, if version1 < version2 return-1, otherwise ret Urn 0.
Assume that the version strings is non-empty and contain only digits and the .
character.
The .
character does not represent a, decimal point and was used to separate number sequences.
For instance, was not "both and 2.5
a half" or "half-to-version three", it is the fifth Second-level revision of the S Econd first-level revision.
Here are an example of version numbers ordering:
0.1 < 1.1 < 1.2 < 13.37
Solution: Split the version string into integers, which can be compared in the past.
classSolution { Public: intCompareversion (stringVersion1,stringVersion2) {Vector<string> VS1 = Split (Version1,'.'); Vector<string> VS2 = Split (Version2,'.'); intS1 = vs1.size (), S2 =vs2.size (); if(S1 = =0&& S2 = =0) return 0; Else if(S1 = =0|| S2 = =0) returnS2 = =0?1: -1; Else { intI, J; for(i =0, j =0; I < S1 && J < S2; ++i, + +j) {if(Stoi (Vs1[i]) < Stoi (Vs2[j]))return-1; if(Stoi (Vs1[i]) > Stoi (vs2[j]))return 1; } while(I <S1)if(Stoi (vs1[i++]) >0)return 1; while(J <S2)if(Stoi (vs2[j++]) >0)return-1; return 0; } }Private: Vector<string> Split (Const string& STR,Const CharDelim) {Vector<string>Res; intStart =0, i =0; for(inti =0; I < str.size (); ++i) {if(Str[i] = =Delim) { stringS (str.begin () + Start, str.begin () +i); if(!s.empty ()) res.push_back (s); Start= i +1; } } stringS (str.begin () +start, Str.end ()); if(!s.empty ()) res.push_back (s); returnRes; }};
[Leetcode]36. Compare version Numbers edition number comparison