The title
I give you two version numbers version1 and version2, please compare them.
A version number consists of one or more revision numbers connected by a ‘.’. Each revision number consists of multiple digits that may contain leading zeros. Each version number contains at least one character. Revision numbers are numbered from left to right, with subscripts 0, the leftmost revision number subscript 0, the next revision number subscript 1, and so on. For example, 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, only the integer values after ignoring any leading zeros are compared. That is, revision number 1 is equal to revision number 001. If the version number does not specify a revision number at a subscript, the revision number is treated as 0. For example, version 1.0 is less than version 1.1 because they have the same revision number with subscript 0, and the revision number with subscript 1 is 0 and 1, respectively, with 0 < 1.
The return rule is as follows:
Return 1 if version1 > version2, -1 if version1 < version2, and 0 otherwise.
Example 1:
Enter: version1 ="1.01", version2 = "1.001"Output:0Explanation: Ignore leading zeros,"01" 和 "001"They all represent the same integer"1"
Copy the code
Example 2:
Enter: version1 ="1.0", version2 = "1.0.0"Output:0Explanation: Version1 does not specify subscript as2Is deemed as"0"
Copy the code
Example 3:
Enter: version1 ="0.1", version2 = "1.1"Output:- 1Explanation: Subscript is version10The revision number of"0"In version2, the subscript is0The revision number of"1" 。0 < 1, so version1 < version2Copy the code
Example 4:
Enter: version1 ="1.0.1", version2 = "1"Output:1
Copy the code
Example 5:
Enter: version1 ="7.5.2.4", version2 = "7.5.3"Output:- 1
Copy the code
Train of thought
1. Split the string by “.”, and then convert each element of the string array to an integer by parseInt, so we have two arrays of integers.
2. Supplement the array. To get the correct result, we supplement the two arrays into an array of equal length, giving the short array push 0;
3. Start the comparison from the 0 bit, return the comparison result if the size is separated, and compare the next bit if it is equal. Returns 0 if they are all equal.
AC code
var compareInt = function(arr1,arr2){
let len = Math.max(arr1.length,arr2.length)
while(arr1.length < len) arr1.push(0)
while(arr2.length < len) arr2.push(0)
for(let i = 0; i < len; i++){
if(arr1[i] > arr2[i]) return 1
else if(arr1[i] < arr2[i]) return -1
}
return 0
}
var compareVersion = function(version1, version2) {
let v1 = version1.split('. ')
let v2 = version2.split('. ')
v1 = v1.map((item) = >Number.parseInt(item))
v2 = v2.map((item) = >Number.parseInt(item))
return compareInt(v1,v2)
};
Copy the code
conclusion
This topic aims to understand the version comparison skills, that is, the same comparison and the absence of completion, think of these two points should be very clear, and version comparison is often used in the project, I hope friends can master this skill oh ~, come on!
This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to view the details of the campaign: juejin.cn/post/693314…