search
HomeJavajavaTutorialHow to compare version numbers using Go Java algorithm

比较版本号

给你两个版本号 version1 和 version2 ,请你比较它们。

  • 版本号由一个或多个修订号组成,各修订号由一个 '.' 连接。每个修订号由 多位数字 组成,可能包含 前导零 。每个版本号至少包含一个字符。

  • 修订号从左到右编号,下标从 0 开始,最左边的修订号下标为 0 ,下一个修订号下标为 1 ,以此类推。例如,2.5.33 和 0.1 都是有效的版本号。

  • 比较版本号时,请按从左到右的顺序依次比较它们的修订号。比较修订号时,只需比较 忽略任何前导零后的整数值 。也就是说,修订号 1 和修订号 001 相等 。

  • 如果版本号没有指定某个下标处的修订号,则该修订号视为 0 。例如,版本 1.0 小于版本 1.1 ,因为它们下标为 0 的修订号相同,而下标为 1 的修订号分别为 0 和 1 ,0

返回规则如下:

如果 version1 > version2 返回 1,

如果 version1 

除此之外返回 0。  

  • 示例 1:

输入:version1 = "1.01", version2 = "1.001"

输出:0

解释:忽略前导零,"01" 和 "001" 都表示相同的整数 "1"

  • 示例 2:

输入:version1 = "1.0", version2 = "1.0.0"

输出:0

解释:version1 没有指定下标为 2 的修订号,即视为 "0"

  • 示例 3:

输入:version1 = "0.1", version2 = "1.1"

输出:-1

解释:version1 中下标为 0 的修订号是 "0",version2 中下标为 0 的修订号是 "1" 。0

提示:

1

version1 和 version2 仅包含数字和 '.'

version1 和 version2 都是 有效版本号

version1 和 version2 的所有修订号都可以存储在 32 位整数

方法一:字符串切割(Java)

我们可以将版本号按照点号分割成修订号,然后从左到右比较两个版本号的相同下标的修订号。在比较修订号时,需要将字符串转换成整数进行比较。

通过调用Java的标准库即可实现字符串切割

class Solution {
    public int compareVersion(String version1, String version2) {
        String[] v1 = version1.split("\\.");
        String[] v2 = version2.split("\\.");
        for (int i = 0; i < v1.length || i < v2.length; ++i) {
            int x = 0, y = 0;
            if (i < v1.length) {
                x = Integer.parseInt(v1[i]);
            }
            if (i < v2.length) {
                y = Integer.parseInt(v2[i]);
            }
            if (x > y) {
                return 1;
            }
            if (x < y) {
                return -1;
            }
        }
        return 0;
    }
}

时间复杂度:O(m+n)

空间复杂度:O(m+n)

方法二:双指针(Go)

方法一需要存储分割后的修订号,为了优化空间复杂度,我们可以在分割版本号的同时解析出修订号进行比较。

比较两个版本号大小,版本号由修订号组成,中间使用'.'分隔,越靠近字符串前边,修订号的优先级越大。当v1 > v2时返回 1,当v1

我们使用两个指针i和j分别指向两个字符串的开头,然后向后遍历,当遇到小数点'.'时停下来,并将每个小数点'.'分隔开的修订号解析成数字进行比较,越靠近前边,修订号的优先级越大。根据修订号大小关系,返回相应的数值。

算法具体流程:

  • 1、定义两个指针 i和j,初始化i = 0,j = 0。

  • 2、两个指针分别遍历两个字符串,将每个小数点'.'分隔开的修订号解析成数字,并进行大小比较:

    • 如果 num1 > num2,返回 1;

    • 如果 num1

  • 3、i++,j++,两个指针都后移一步,进行下一轮的修订号解析比较。

  • 4、如果遍历完两个字符串都没有返回相应结果,说明两个字符串相等,返回0。

func compareVersion(version1, version2 string) int {
    n, m := len(version1), len(version2)
    i, j := 0, 0
    for i < n || j < m {
        x := 0
        for ; i < n && version1[i] != &#39;.&#39;; i++ {
            x = x*10 + int(version1[i]-&#39;0&#39;)
        }
        i++ // 跳过点号
        y := 0
        for ; j < m && version2[j] != &#39;.&#39;; j++ {
            y = y*10 + int(version2[j]-&#39;0&#39;)
        }
        j++ // 跳过点号
        if x > y {
            return 1
        }
        if x < y {
            return -1
        }
    }
    return 0
}

时间复杂度:O(m+n)

空间复杂度:O(1)

The above is the detailed content of How to compare version numbers using Go Java algorithm. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools