1. Topics
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 Second First-level revision.
Here are an example of version numbers ordering:
0.1 < 1.1 < 1.2 < 13.37
2. Solutions
Class Solution {public:vector<string> getallversionstring (string version1) {vector<string> AllVersi On1; String OneVersion1; for (int i = 0; i < version1.size (); ++i) {if (version1[i]! = '. ') {Oneversion1.push_back (version1[i]); }else{Allversion1.push_back (OneVersion1); Oneversion1.clear (); }} allversion1.push_back (OneVersion1); return allVersion1; } int Comparever (vector<string> allVersion1, vector<string> allVersion2) {for (int i = 0; i < ALLV Ersion1.size (); ++i) {if (I < allversion2.size ()) {if (Atoi (Allversion1[i].c_str ()) < Atoi (AllVersion2 [I].c_str ())) {return-1; }else if (Atoi (Allversion1[i].c_str ()) > Atoi (Allversion2[i].c_str ())) {return 1; }}else{if (Atoi (AllversioN1[I].C_STR ()) > 0) {return 1; }}} return 0; } int Compareversion (string version1, String version2) {vector<string> AllVersion1 = getallversionstring ( Version1); vector<string> AllVersion2 = getallversionstring (Version2); int result = 0; if (Allversion1.size () >= allversion2.size ()) {result = Comparever (AllVersion1, AllVersion2); }else if (allversion2.size () > Allversion1.size ()) {result =-comparever (AllVersion2, AllVersion1); } return result; }};
there seems to be no split function in C + +, it feels like I'm also writing trouble.
http://www.waitingfy.com/archives/1683
Leetcode Compare Version Numbers