`

Leetcode - 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 . character does not represent a decimal point and is used to separate number sequences.
For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
Here is an example of version numbers ordering:

[分析] 两种方法的主要思路是一致的,比较点号分割出来的每一个子串对应的数字。两个版本串的点号可能不一样多,为了正确比较诸如(1.0, 1) 或者(1.0.1, 1)这样的情况,实现时点号少的串可以视为后面都是0,一直比较到长串结束。Method1 需要额外空间,且注意String的split方法不能使用点号进行分割。Method2 不需要额外空间。

    // Method 1
    public int compareVersion(String version1, String version2) {
        if (version1 != null && version2 != null) {
            version1 = version1.replace('.', ':');
            version2 = version2.replace('.', ':');
            String[] num1 = version1.split(":");
            String[] num2 = version2.split(":");
            int val1 = 0;
            int val2 = 0;
            int i = 0;
            while (i < num1.length || i < num2.length) {
                val1 = 0;
                val2 = 0;
                if (i < num1.length)
                    val1 = Integer.parseInt(num1[i]);
                if (i < num2.length)
                    val2 = Integer.parseInt(num2[i]);
                if (val1 < val2)
                    return -1;
                else if (val1 > val2)
                    return 1;
                i++;
            }
            return 0;
        } else if (version1 != null) {
            return 1;
        } else if (version2 != null) {
            return -1;
        } else {
            return 0;
        }
    }
    // Method 2
    public int compareVersion(String version1, String version2) {
        if (version1 != null && version2 != null) {
            int p1 = 0, p2 = 0;
            int nextDot1 = -1, nextDot2 = -1;
            int n1 = version1.length(), n2 = version2.length();
            int sub1 = 0, sub2 = 0;
            while (p1 < n1 || p2 < n2) {
                nextDot1 = version1.indexOf('.', p1);
                nextDot2 = version2.indexOf('.', p2);
                if (p1 < n1)
                    sub1 = Integer.valueOf(version1.substring(p1, nextDot1 != -1 ? nextDot1 : n1));
                else
                    sub1 = 0;
                if (p2 < n2)
                    sub2 = Integer.valueOf(version2.substring(p2, nextDot2 != -1 ? nextDot2 : n2));
                else
                    sub2 = 0;
                if (sub1 > sub2)
                    return 1;
                else if (sub1 < sub2)
                    return -1;
                p1 = nextDot1 != -1 ? (nextDot1 + 1) : n1;
                p2 = nextDot2 != -1 ? (nextDot2 + 1) : n2;
            }
            return 0;
        } else if (version1 == null) {
            return -1;
        } else if (version2 == null) {
            return 1;
        } else {
            return 0;
        }
    } 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics