因为输入的数字可能很长,不能用int装,引入大数操作类BigInteger,以字符串存储;
import java.math.BigInteger;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 比较版本号
* @param version1 string字符串
* @param version2 string字符串
* @return int整型
*/
public int compare (String version1, String version2) {
// write code here
String[] versionStr1=version1.split("\\.");
String[] versionStr2=version2.split("\\.");
int length=versionStr1.length>versionStr2.length?versionStr1.length:versionStr2.length;
for(int i=0;i<length;i++){
BigInteger versionInt1=i<versionStr1.length?new BigInteger(versionStr1[i]):new BigInteger("0");
BigInteger versionInt2=i<versionStr2.length?new BigInteger(versionStr2[i]):new BigInteger("0");
if(versionInt1.compareTo(versionInt2)<0) return -1;
else if(versionInt1.compareTo(versionInt2)>0) return 1;
}
return 0;
}
}