比较两个版本号
版本号是一个字符串,用于标识软件产品的唯一状态。版本号看起来像 abcd,其中 a、b 等是数字,所以版本号是一个字符串,其中的数字用点分隔。这些数字通常代表从主要到次要的层次结构(a 是主要的,d 是次要的)。
在这个问题中,我们得到了两个版本号。我们需要对它们进行比较,得出哪个是最新的版本号(即哪个版本号更大)。
例子:
Input:
V1 = “1.0.31”
V2 = “1.0.27”
Output: v1 is latest
Because V2 < V1
Input:
V1 = “1.0.10”
V2 = “1.0.27”
Output: v2 is latest
Because V1 < V2
方法:由于是点,无法直接比较,但版本可以比较数字部分,然后可以找到最新版本。遍历字符串并分离数字部分并进行比较。如果相等,则比较下一个数字部分,依此类推,直到它们不同,否则将它们标记为相等。
实现一个方法来比较两个版本。如果有两个以上的版本,那么下面的 versionCompare 方法可以作为 sort 方法的比较方法,它会根据指定的比较对所有版本进行排序。
C++
// C/C++ program to compare
// two version number
#include
using namespace std;
// Method to compare two versions.
// Returns 1 if v2 is smaller, -1
// if v1 is smaller, 0 if equal
int versionCompare(string v1, string v2)
{
// vnum stores each numeric
// part of version
int vnum1 = 0, vnum2 = 0;
// loop until both string are
// processed
for (int i = 0, j = 0; (i < v1.length()
|| j < v2.length());) {
// storing numeric part of
// version 1 in vnum1
while (i < v1.length() && v1[i] != '.') {
vnum1 = vnum1 * 10 + (v1[i] - '0');
i++;
}
// storing numeric part of
// version 2 in vnum2
while (j < v2.length() && v2[j] != '.') {
vnum2 = vnum2 * 10 + (v2[j] - '0');
j++;
}
if (vnum1 > vnum2)
return 1;
if (vnum2 > vnum1)
return -1;
// if equal, reset variables and
// go for next numeric part
vnum1 = vnum2 = 0;
i++;
j++;
}
return 0;
}
// Driver method to check above
// comparison function
int main()
{
string version1 = "1.0.3";
string version2 = "1.0.7";
if (versionCompare(version1, version2) < 0)
cout << version1 << " is smaller\n";
else if (versionCompare(version1, version2) > 0)
cout << version2 << " is smaller\n";
else
cout << "Both version are equal\n";
return 0;
}
Java
// Java program to compare two version number
import java.util.*;
class GFG {
// Method to compare two versions.
// Returns 1 if v2 is
// smaller, -1 if v1 is smaller, 0 if equal
static int versionCompare(String v1, String v2)
{
// vnum stores each numeric part of version
int vnum1 = 0, vnum2 = 0;
// loop until both String are processed
for (int i = 0, j = 0; (i < v1.length()
|| j < v2.length());) {
// Storing numeric part of
// version 1 in vnum1
while (i < v1.length()
&& v1.charAt(i) != '.') {
vnum1 = vnum1 * 10
+ (v1.charAt(i) - '0');
i++;
}
// storing numeric part
// of version 2 in vnum2
while (j < v2.length()
&& v2.charAt(j) != '.') {
vnum2 = vnum2 * 10
+ (v2.charAt(j) - '0');
j++;
}
if (vnum1 > vnum2)
return 1;
if (vnum2 > vnum1)
return -1;
// if equal, reset variables and
// go for next numeric part
vnum1 = vnum2 = 0;
i++;
j++;
}
return 0;
}
// Driver method to check above
// comparison function
public static void main(String[] args)
{
String version1 = "1.0.3";
String version2 = "1.0.7";
if (versionCompare(version1, version2) < 0)
System.out.println(version1 + " is smaller");
else if (versionCompare(version1, version2) > 0)
System.out.println(version2 + " is smaller");
else
System.out.println("Both version are equal");
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python program to compare two version number
# Method to compare two versions.
# Return 1 if v2 is smaller,
# -1 if v1 is smaller,,
# 0 if equal
def versionCompare(v1, v2):
# This will split both the versions by '.'
arr1 = v1.split(".")
arr2 = v2.split(".")
n = len(arr1)
m = len(arr2)
# converts to integer from string
arr1 = [int(i) for i in arr1]
arr2 = [int(i) for i in arr2]
# compares which list is bigger and fills
# smaller list with zero (for unequal delimiters)
if n>m:
for i in range(m, n):
arr2.append(0)
else if m>n:
for i in range(n, m):
arr1.append(0)
# returns 1 if version 1 is bigger and -1 if
# version 2 is bigger and 0 if equal
for i in range(len(arr1)):
if arr1[i]>arr2[i]:
return 1
else if arr2[i]>arr1[i]:
return -1
return 0
# Driver program to check above comparison function
version1 = "1.0.3"
version2 = "1.0.7"
ans = versionCompare(version1, version2)
if ans < 0:
print (version1 + " is smaller")
else if ans > 0:
print (version2 + " is smaller")
else:
print ("Both versions are equal")
# This code is contributed by Nikhil Kumar Singh(nickzuck_007)
# and improved by Tuhin Das (tuhindas221b)
C#
// C# program to compare
// two version number
using System;
class GFG
{
// Method to compare two versions.
// Returns 1 if v2 is smaller, -1
// if v1 is smaller, 0 if equal
static int versionCompare(string v1, string v2)
{
// vnum stores each numeric
// part of version
int vnum1 = 0, vnum2 = 0;
// loop until both string are
// processed
for (int i = 0, j = 0; (i < v1.Length || j < v2.Length);)
{
// storing numeric part of
// version 1 in vnum1
while (i < v1.Length && v1[i] != '.')
{
vnum1 = vnum1 * 10 + (v1[i] - '0');
i++;
}
// storing numeric part of
// version 2 in vnum2
while (j < v2.Length && v2[j] != '.')
{
vnum2 = vnum2 * 10 + (v2[j] - '0');
j++;
}
if (vnum1 > vnum2)
return 1;
if (vnum2 > vnum1)
return -1;
// if equal, reset variables and
// go for next numeric part
vnum1 = vnum2 = 0;
i++;
j++;
}
return 0;
}
// Driver code
static void Main()
{
string version1 = "1.0.3";
string version2 = "1.0.7";
if (versionCompare(version1, version2) < 0)
Console.WriteLine(version1 + " is smaller");
else if (versionCompare(version1, version2) > 0)
Console.WriteLine(version2 + " is smaller");
else
Console.WriteLine("Both version are equal");
}
}
// This code is contributed by divyeshrabadiya07.
Javascript
输出:
1.0.3 is greater
复杂性分析:
- 时间复杂度: O(n),其中 n 是字符串的长度。
只需要遍历一次字符串。 - 辅助空间: O(1)。
因为不需要额外的空间。