Integer.valueOf("01") can handle conversion from string to integer.
public int compareVersion(String version1, String version2) {
String[] version1Res = version1.split("\\.");
String[] version2Res = version2.split("\\.");
int idx = Math.max(version1Res.length, version2Res.length);
int i=0;
while (i < idx) {
int c1 = i < version1Res.length ? Integer.valueOf(version1Res[i]):0;
int c2 = i < version2Res.length ? Integer.valueOf(version2Res[i]):0;
if (c1>c2) {
return 1;
} else if (c1<c2) {
return -1;
}
i++;
}
return 0;
}
我开始是选了 minLength来比较,之后再去处理length不相等的情况,很麻烦,有些case还过不了,撸主这样很是方便,攒一个
ReplyDeleteHey Keep in touch. Need some partners.
Delete