public int compareVersion(String version1, String version2) {
String[] sp1 = version1.split("\\.");
String[] sp2 = version2.split("\\.");
int n1 = sp1.length, n2 = sp2.length;
int i = 0, j = 0;
while (i < n1 || j < n2) {
int s1 = 0, s2 = 0;
if (i < n1) s1 = Integer.parseInt(sp1[i++]);
if (j < n2) s2 = Integer.parseInt(sp2[j++]);
if (s1 != s2) return s1 < s2 ? -1 : 1;
}
return 0;
}
String.split()
方法public int compareVersion(String version1, String version2) {
int n1 = version1.length(), n2 = version2.length();
int i = 0, j = 0;
while (i < n1 || j < n2) {
int s1 = 0, s2 = 0;
int t1 = i, t2 = j;
while (i < n1 && version1.charAt(i) != '.') i++;
while (j < n2 && version2.charAt(j) != '.') j++;
if (t1 != i) s1 = Integer.parseInt(version1.substring(t1, i));
if (t2 != j) s2 = Integer.parseInt(version2.substring(t2, j));
if (s1 != s2) return s1 < s2 ? -1 : 1;
if (i < n1) i++;
if (j < n2) j++;
}
return 0;
}