Compare numbers version1 and Version2.
If Version1 > Version2 return 1, if Version1 < Version2 return-1, otherwise return 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, 2.5 are not "both and a half" or "half to version three", it is the fifth Second-level revision of the SEC Ond first-level revision.
Here are an example of version numbers ordering:
0.1 < 1.1 < 1.2 < 13.37
Problem Solving Ideas:
Split first, then int comparison, Java implementation is as follows:
static public int compareversion (string version1, String version2) { string[] s1=version1.split ("\ \."); String[] S2=version2.split ("\ \"); Int[] Num1=new Int[math.max (S1.length, S2.length)]; Int[] Num2=new Int[math.max (S1.length, S2.length)]; for (int i=0;i<s1.length;i++) num1[i]= integer.valueof (S1[i]); for (int i=0;i<s2.length;i++) num2[i]= integer.valueof (S2[i]); for (int i=0;i<num1.length;i++) { if (Num1[i]>num2[i]) return 1; else if (Num1[i]<num2[i]) return-1; } return 0; }
Java for Leetcode 165 Compare Version Numbers