Compare numbers version1 and version1.
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:
2, using "." When separating, add "\ \."
3, the default is 0 when the array is not assigned a value
Package Leetcode;public class Compareversionnumbers {public int compareversion (string version1, String version2) { C1/>string[] Ver1=version1.split ("\ \"); String[] Ver2=version2.split ("\ \"); int Maxlength;if (ver1.length>=ver2.length) { maxlength=ver1.length; } else{ maxlength=ver2.length; } int[] Ints1 = new Int[maxlength]; for (int i=0; i < maxlength; i++) { Ints1[i] = Integer.parseint (Ver1[i]); } int[] ints2 = new Int[maxlength]; for (int i=0; i < maxlength; i++) { Ints2[i] = Integer.parseint (Ver2[i]); } for (int i=0;i<maxlength;i++) { if (Ints1[i]>ints2[i]) { return 1; } else if (Ints1[i]<ints2[i]) { return-1; } } return 0;} }
Leetcode Compare Version Numbers