/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 比较版本号
* @param version1 string字符串
* @param version2 string字符串
* @return int整型
*/
function compare( version1 , version2 ) {
// write code here
const num1 = version1.split('.');
const num2 = version2.split('.');
for(let i=0; i<num1.length; i++) {
// 如果为空补充0
let s1 = num1[i] || '0';
let s2 = num2[i] || '0';
// 如果为'01'这种转换为'1'
if(s1.startsWith('0') && s1.length > 1) {
s1 = num1[i].slice(1, num1[i].length);
}
if(s2.startsWith('0') && s2.length > 1) {
s2 = num2[i].slice(1, num2[i].length);
}
s1 = parseInt(s1);
s2 = parseInt(s2);
if(s1>s2) {
return 1
} else if(s1<s2) {
return -1;
}
}
return 0;
}
module.exports = {
compare : compare
};