# Compare Version Numbers LeetCode Solution

Here, We see Compare Version Numbers LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

# List of all LeetCode Solution

## Problem Statement

Given two version numbers, `version1` and `version2`, compare them.

Version numbers consist of one or more revisions joined by a dot `'.'`. Each revision consists of digits and may contain leading zeros. Every revision contains at least one character. Revisions are 0-indexed from left to right, with the leftmost revision being revision 0, the next revision being revision 1, and so on. For example `2.5.33` and `0.1` are valid version numbers.

To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions `1` and `001` are considered equal. If a version number does not specify a revision at an index, then treat the revision as `0`. For example, version `1.0` is less than version `1.1` because their revision 0s are the same, but their revision 1s are `0` and `1` respectively, and `0 < 1`.

Return the following:

• If `version1 < version2`, return `-1`.
• If `version1 > version2`, return `1`.
• Otherwise, return `0`.

Example 1:
Input: version1 = “1.01”, version2 = “1.001”
Output: 0
Explanation: Ignoring leading zeroes, both “01” and “001” represent the same integer “1”.

Example 2:
Input: version1 = “1.0”, version2 = “1.0.0”
Output: 0
Explanation: version1 does not specify revision 2, which means it is treated as “0”.

Example 3:
Input: version1 = “0.1”, version2 = “1.1”
Output: -1
Explanation: version1’s revision 0 is “0”, while version2’s revision 0 is “1”. 0 < 1, so version1 < version2.

## Compare Version Numbers LeetCode SolutionC++

``````class Solution {
public:
int compareVersion(string version1, string version2) {
int ans = 0;
int n1 = version1.size(), n2 = version2.size();
for(int i=0,j=0; i<n1 || j<n2; ++i, ++j) {
string s1 = "", s2 = "";
while(i<n1 && version1[i] != '.') {
if(s1.size() == 0 && version1[i] == '0') {
++i; continue;
}
s1 += version1[i++];
}
while(j<n2 && version2[j] != '.') {
if(s2.size() == 0 && version2[j] == '0') {
++j; continue;
}
s2 += version2[j++];
}
if(s1.size() < s2.size()) return -1;
else if(s2.size() < s1.size()) return 1;
ans = s1.compare(s2);
if(ans < 0) return -1;
else if(ans > 0) return 1;
}
return 0;
}
};```Code language: PHP (php)```

## Compare Version Numbers LeetCode SolutionJava

``````class Solution {
public int compareVersion(String version1, String version2) {
int temp1 = 0,temp2 = 0;
int len1 = version1.length(),len2 = version2.length();
int i = 0,j = 0;
while(i<len1 || j<len2) {
temp1 = 0;
temp2 = 0;
while(i<len1 && version1.charAt(i) != '.') {
temp1 = temp1*10 + version1.charAt(i++)-'0';
}
while(j<len2 && version2.charAt(j) != '.') {
temp2 = temp2*10 + version2.charAt(j++)-'0';
}
if(temp1>temp2) return 1;
else if(temp1<temp2) return -1;
else {
i++;
j++;
}
}
return 0;
}
}```Code language: JavaScript (javascript)```

## Compare Version Numbers SolutionJavaScript

``````var compareVersion = function(version1, version2) {
var v1Array = version1.split('.');
var v2Array = version2.split('.');
var length = Math.max(v1Array.length, v2Array.length);
for (var i = 0; i < length; i++) {
var num1 = parseInt(v1Array[i]) || 0;
var num2 = parseInt(v2Array[i]) || 0;
if (num1 == num2) {
continue;
}
return num1 > num2 ? 1 : -1;
}
return 0;
};```Code language: JavaScript (javascript)```

## Compare Version Numbers SolutionPython

``````class Solution(object):
def compareVersion(self, version1, version2):
main1, _, rest1 = ('0' + version1).partition('.')
main2, _, rest2 = ('0' + version2).partition('.')
return cmp(int(main1), int(main2)) or \
len(rest1+rest2) and self.compareVersion(rest1, rest2)``````
Scroll to Top