这道题通过“.”划分成数组,在一个一个数组比较
public class Solution {
public int compareVersion(String version1, String version2) {
String []versionl1 = version1.split("\\.");
String []versionl2 = version2.split("\\.");
int i = 0;
while (i<versionl1.length&&i<versionl2.length){
if (Integer.valueOf(versionl1[i])>Integer.valueOf(versionl2[i])){
return 1;
}else if (Integer.valueOf(versionl1[i])==Integer.valueOf(versionl2[i])){
i++;
}else {
return -1;
}
}
if (i<versionl1.length){
for (;i<versionl1.length;i++){
if (Integer.valueOf(versionl1[i])>0)return 1;
}
}
if (i<versionl2.length){
for (;i<versionl2.length;i++){
if (Integer.valueOf(versionl2[i])>0)return -1;
}
}
return 0;
}
}