LeetCode:Compare Version Numbers - 比较版本号

1、题目名称

Compare Version Numbers(比较版本号)

2、题目地址

https://leetcode.com/problems/compare-version-numbers/

3、题目内容

英文:Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.

中文:比较两个版本version1和version2,如果version1晚于version2,则返回1,如果version1早于version2,则返回-1,其他情况下返回0

4、解题方法

本题的解题思路就是,先将两个版本号按“.”分段,然后逐段比较。需要说明的是本题要考虑一个版本号与另一个版本号的前半段完全相同的情况(如“1.0”和“1.0.1”中“1.0.1”较大,“1.0”和“1.0.0”一样大)。Java代码如下:

/**
 * 功能说明:LeetCode 165 - Compare Version Numbers
 * 开发人员:Tsybius2014
 * 开发时间:2015年9月17日
 */
public class Solution {
    
    /**
     * 比较版本号
     * @param version1 版本号1
     * @param version2 版本号2
     * @return
     */
    public int compareVersion(String version1, String version2) {
       
        String[] versionArray1 = version1.split("\\.");
        String[] versionArray2 = version2.split("\\.");
        

        int len1 = versionArray1.length;
        int len2 = versionArray2.length;
        int len = len1 <= len2 ? len1 : len2;

        //共有版本号部分,从前向后比较对应位置数字
        int x1, x2;
        for (int i = 0; i < len; i++) {
            x1 = Integer.parseInt(versionArray1[i]);
            x2 = Integer.parseInt(versionArray2[i]);
            if (x1 > x2) {
                return 1;
            } else if (x1 < x2) {
                return -1;
            }
        }
        
        //共有版本号相等的情况下,谁的版本号段数更多且多余部分不全为0,谁的版本更新
        if (len1 > len2) {
            for (int i = len; i < len1; i++) {
                if (Integer.parseInt(versionArray1[i]) > 0) {
                    return 1;
                }
            }
        } else if (len1 < len2) {
            for (int i = len; i < len2; i++) {
                if (Integer.parseInt(versionArray2[i]) > 0) {
                    return -1;
                }
            }
        }
        
        return 0;
    }
}

END

时间: 2024-10-16 21:53:31

LeetCode:Compare Version Numbers - 比较版本号的相关文章

[LeetCode] Compare Version Numbers 版本比较

Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character.The . characte

[LeetCode] Compare Version Numbers

Question: Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character.The 

LeetCode—Compare Version Numbers

Compare two version numbers version1 and version1. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character. The . charac

165. Compare Version Numbers比较版本号的大小

[抄题]: Compare two version numbers version1 and version2.If version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character.The . cha

No.165 Compare Version Numbers

No.165 Compare Version NumbersCompare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and

【leetcode 字符串处理】Compare Version Numbers

[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Compare two version numbers version1 and version1. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume

165. Compare Version Numbers - LeetCode

Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: public int compareVersion(String version1, String version2) { String[] v1Arr = version1.split("\\."); String[] v2Arr = version2.split("\\.&qu

【leetcode】Compare Version Numbers

Compare Version Numbers Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . 

leetCode 165. Compare Version Numbers 字符串

165. Compare Version Numbers Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and t