Skip to content

165比较版本号

code

javascript
/**
 * @param {string} version1
 * @param {string} version2
 * @return {number}
 */
var compareVersion = function(version1, version2) {
    let v1 = version1.split('.').map((item) => Number(item));
    let v2 = version2.split('.').map((item) => Number(item));
    let ind = 0;
    if(v1.length !== v2.length){
        if(v1.length < v2.length) {
            let temp1 = v2.length - v1.length;
            for(let i = 0; i < temp1; i++){
                v1.push(0);
            }
        } else {
            let temp2 = v1.length - v2.length;
            for(let j = 0; j < temp2; j++){
                v2.push(0);
            }
        }
    }
    while(ind < v1.length) {
        if(v1[ind] < v2[ind]){
            return -1;
        } 
        if(v1[ind] > v2[ind]){
            return 1;
        } else {
            ind++;
        }
    }
    return 0;
};

总结

  1. 感觉这题挺蠢的,拆分,补齐就完事了?虽然我也没看出啥别的,感觉就是在考str.split('.')api