165
class Solution {
int res = 0;
int flag = 1;
String[] versionlong;
String[] versionshort;
public int compareVersion(String version1, String version2) {
String[] version1_sep = version1.split("\\.");
String[] version2_sep = version2.split("\\.");
int index = Math.min(version1_sep.length, version2_sep.length);
int lenMax = Math.max(version1_sep.length, version2_sep.length);
swap(version1, version2, version1_sep, version2_sep);
int i;
for(i = 0; i < index; i++){
res = (Integer.parseInt(versionlong[i]) - Integer.parseInt(versionshort[i]));
if(res < 0){
res = -1;
break;
} else if(res > 0){
res = 1;
break;
}
}
if(res == 0){
for(; i < lenMax; i++){
if(Integer.parseInt(versionlong[i]) > 0){
res = 1;
break;
}
}
}
return res*flag;
}
public void swap(String version1, String version2, String[] version1_sep, String[] version2_sep){
if(version1.length() >= version2.length()){
versionlong = version1_sep;
versionshort = version2_sep;
flag = 1;
} else {
versionlong = version2_sep;
versionshort = version1_sep;
flag = -1;
}
}
}