Compare Version Numbers
Medium · strings
A version number is a list of revisions written as whole numbers and separated by single dots, for example "2.14.0". Each revision is a non-negative integer, and it may be written with leading zeros: "007" is the same revision as "7".
Compare two version numbers version1 and version2 by looking at their revisions from left to right, as numbers. The first revision that differs decides the result.
If one version has fewer revisions than the other, treat each missing revision as 0, so "1.0" and "1" are equal.
Return -1 if version1 is smaller, 1 if version1 is larger, and 0 if they are equal.
Examples
Example 1
Input: version1 = "1.2", version2 = "1.10"
Output: -1
The first revisions are equal. The second revisions are 2 and 10, as numbers, and 2 is smaller.
Example 2
Input: version1 = "1.01", version2 = "1.001"
Output: 0
Leading zeros are ignored, so both second revisions are 1.
Example 3
Input: version1 = "3.0.1", version2 = "3"
Output: 1
The missing second and third revisions of the right version count as 0, and 1 is larger than 0.
Constraints
- 1 <= len(version1), len(version2) <= 10^4
- Each version contains only digits and the dot character, and it neither starts nor ends with a dot.
- No two dots are next to each other, so every revision has at least one digit.
- Every revision, once leading zeros are ignored, is at most 10^9, and is written with at most 1000 characters.