DEV Community

Karleb
Karleb

Posted on

#165. Compare Version Numbers

https://leetcode.com/problems/compare-version-numbers/description/?envType=daily-question&envId=2024-05-03

function compareVersion(version1, version2) {
    const v1 = version1.split('.')
    const v2 = version2.split('.')
    const maxLength = Math.max(version1.length, version2.length)

    for (let i = 0; i < maxLength; i++) {
        const num1 = i < v1.length ? Number(v1[i]) : 0
        const num2 = i < v2.length ? Number(v2[i]) : 0

        if (num1 < num2) return -1
        if (num1 > num2) return 1
    }

    return 0
}


Enter fullscreen mode Exit fullscreen mode

Top comments (0)