题目
https://leetcode-cn.com/problems/compare-version-numbers/solution/
思路
找到每个.分割的版本号,然后转换成数字进行比较
code
/**
* @param {string} version1
* @param {string} version2
* @return {number}
*/
var compareVersion = function (version1, version2) {
let len1 = version1.length;
let len2 = version2.length;
let i,j;
i = j = 0;
let n1, n2;
n1 = n2 = 0;
while (i < len1 || j< len2){
// 字符串转换成数字进行比较
while (i<len1 && version1[i] != '.') n1 += n1 * 10 + +(version1[i++]);
while (j < len2 && version2[j] != '.') n2 += n2 * 10 + +(version2[j++]);
if (n1>n2){
return 1;
}else if (n1<n2){
return -1;
}else{
n1 = n2 = 0;
i++; j++;
}
}
return 0;
};