Leetcode 165. Compare version numbers

Time:2021-10-20

subject

I’ll give you two version numbers, Version1 and version2. Please compare them.

The version number consists of one or more revision numbers, and each revision number is connected by a ‘.’. Each revision number consists of multiple digits and may contain leading zeros. Each version number contains at least one character. The revision number is numbered from left to right, the subscript starts from 0, the leftmost revision number subscript is 0, the next revision number subscript is 1, and so on. For example, both 2.5.33 and 0.1 are valid version numbers.

When comparing version numbers, compare their revision numbers from left to right. When comparing revision numbers, you only need to compare integer values that ignore any leading zeros. That is, revision 1 and revision 001 are equal. If the revision number at a subscript is not specified in the version number, the revision number is regarded as 0. For example, version 1.0 is smaller than version 1.1 because they have the same revision number with subscript 0, while the revision numbers with subscript 1 are 0 and 1 respectively, 0 < 1.

The return rules are as follows:

If Version1 > version2 returns 1,
If Version1 < version2 returns – 1,
In addition, 0 is returned.

Example 1:

Input: Version1 = “1.01”, version2 = “1.001”
Output: 0
Explanation: ignoring leading zeros, “01” and “001” both represent the same integer “1”

Example 2:

Input: Version1 = “1.0”, version2 = “1.0.0”
Output: 0
Explanation: Version1 does not specify a revision number with subscript 2, which is regarded as “0”

Example 3:

Input: Version1 = “0.1”, version2 = “1.1”
Output: – 1
Explanation: the revision number with subscript 0 in Version1 is “0”, and the revision number with subscript 0 in version2 is “1”. 0 < 1, so Version1 < version2
Example 4:

Input: Version1 = “1.0.1”, version2 = “1”
Output: 1
Example 5:

Input: Version1 = “7.5.2.4”, version2 = “7.5.3”
Output: – 1

Tips:

1 <= version1.length, version2.length <= 500
Version1 and version2 contain only numbers and ‘.’
Version1 and version2 are valid version numbers
All revision numbers of Version1 and version2 can be stored in 32-bit integers

Source:

Leetcode
Link: https://leetcode-cn.com/probl…
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

Problem solution

Directly divide the two version numbers into arrays according to ‘.’ and compare the values of each digit. Note that the lengths of the two version numbers may be different, so supplement 0 for the shorter version number. The specific code is as follows:

code

var compareVersion = function(version1, version2) {
    version1 = version1.split('.');
    version2 = version2.split('.');
    for(let i = 0; i < Math.max(version1.length,version2.length); i++){
        if(version1[i] == undefined) version1[i] = 0;
        if(version2[i] == undefined) version2[i] = 0;
        if(parseInt(version1[i]) > parseInt(version2[i])){
            return 1;
        }else if(parseInt(version1[i]) < parseInt(version2[i])){
            return -1;
        }
    }
    return 0;
};

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]