【LeetCode】165. Compare Version Numbers 解题报告

来源:互联网 发布:管家婆软件免费版教程 编辑:程序博客网 时间:2024/06/14 16:42

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51439523


Subject

出处:https://leetcode.com/problems/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:

0.1 < 1.1 < 1.2 < 13.37


Explain

该题目的意思是比较【版本号】。版本号只是有“数字”和”.“组成

if version1 > version2 return 1
if version1 < version2 return -1

其他情况返回0.


Solution

solution 1

我的思路是通过split方法分割开,然后转成int值之后,一次往后比较。

    /**     * 小数点隔开的数字依次比较     *      * @author jacksen     *      * @param version1     * @param version2     * @return     */    public static int compareVersion2(String version1, String version2) {        String[] ver1 = version1.split("\\.");        String[] ver2 = version2.split("\\.");        int length1 = ver1.length;        int length2 = ver2.length;        int maxLen = Math.max(length1, length2);        for (int i = 0; i < maxLen; i++) {            int temp1 = 0, temp2 = 0;            if (i < length1) {                temp1 = Integer.parseInt(ver1[i]);            }            if (i < length2) {                temp2 = Integer.parseInt(ver2[i]);            }            if (temp1 > temp2) {                return 1;            } else if (temp1 < temp2) {                return -1;            }        }        return 0;    }

因为采用了split和parseInt两个方法,效率上面不是太高。leetcode平台上虽然通过测试,但是Run Time是4ms


solution 2

第二种方案不采用split和parseInt方法。而是通过循环判断字符,不等于’.‘的字符减去字符’0‘即可得到当前位置的数值。然后每次遇到’.‘去进行比较。

    /**     * 同样是小数点隔开的数字依次比较,不过不是使用interger.parseInt()进行转换。而是采用 -'0'的方法。     *      * @author from Internet     *      * @param version1     * @param version2     * @return     */    public static int compareVersion3(String version1, String version2) {        int temp1;        int temp2;        int i = 0;        int j = 0;        while (i < version1.length() || j < version2.length()) {            temp1 = 0;            temp2 = 0;            //            while (i < version1.length() && '.' != version1.charAt(i)) {                temp1 = temp1 * 10 + (version1.charAt(i) - '0');                i++;            }            i++;            //            while (j < version2.length() && '.' != version2.charAt(j)) {                temp2 = temp2 * 10 + (version2.charAt(j) - '0');                j++;            }            j++;            //            if (temp1 > temp2) {                return 1;            }            if (temp1 < temp2) {                return -1;            }        }        return 0;    }

第二种方法的效率较高,Run Time 是1ms。


bingo~~

1 0
原创粉丝点击