
Given two version strings, version1 and version2, compare them. A version string consists of revisions separated by dots '.'. The value of the revision is its integer conversion ignoring leading zeros.
To compare version strings, compare their revision values in left-to-right order. If one of the version strings has fewer revisions, treat the missing revision values as 0.
Return the following:
If version1 < version2, return -1.
If version1 > version2, return 1.
Otherwise, return 0.
int형 배열로 변환 후, 두 배열의 길이를 똑같게 만들어준다.var compareVersion = function (version1, version2) {
var tmp1 = [];
var tmp2 = [];
for (let x of version1.split('.')) {
tmp1.push(parseInt(x));
}
for (let x of version2.split('.')) {
tmp2.push(parseInt(x));
}
const n = tmp1.length;
const m = tmp2.length;
let diff = Math.abs(n - m);
if (n < m) {
let temp = Array(diff).fill(0);
tmp1.push(...temp);
} else if (n > m) {
let temp = Array(diff).fill(0);
tmp2.push(...temp);
}
for (let i = 0; i < tmp1.length; i++) {
if (tmp1[i] > tmp2[i]) {
return 1;
} else if (tmp1[i] < tmp2[i]) {
return -1;
}
}
return 0;
};
언어를
C++,Python,JS를 다 하다보니 함수가 조금씩 헷갈림
cpp의.length()랑, js의.length처럼...