Problem Description:
Compare numbers version1 andversion1.
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
Basic idea:
From high to low recursive solution. Note Special case (1,1.0);
Code:
public int Compareversion (string version1, String version2) {//java if (version1.equals (Version2)) r Eturn 0; int Fversion1, Fversion2; String Sversion1,sversion2; if (Version1.contains (".")) {int pos = Version1.indexof ("."); Fversion1 = integer.valueof (version1.substring (0,pos)); Sversion1 = version1.substring (Pos+1,version1.length ()); } else {fversion1 = integer.valueof (Version1); Sversion1 = "0"; } if (Version2.contains (".")) {int pos = Version2.indexof ("."); Fversion2 = integer.valueof (version2.substring (0,pos)); Sversion2 = version2.substring (Pos+1,version2.length ()); } else {fversion2 = integer.valueof (Version2); Sversion2 = "0"; } if (Fversion1 > Fversion2) return 1; else if (Fversion1 < fversion2) return-1; else return compareversion (Sversion1, Sversion2); }
[Leetcode] Compare Version Numbers