资讯专栏INFORMATION COLUMN

[LeetCode] Compare Version Numbers

jzman / 3358人阅读

摘要:首先找整数部分的坐标段,和都指向初值,令和一直向后遍历到小数点为止。然后用将的整数段转化为数值,进行比较若结果为大于或小于关系,直接返回结果若结果为相等,进行小数部分的比较。

Problem

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
Note

看到一个巧妙的解法,分享一下:
version1version2都转换为char array,然后分别用两个指针s1, i1s2, i2展开循环。
首先找整数部分的坐标段,s1s2都指向初值0,令i1i2一直向后遍历到小数点为止。
然后用Integer.valueOf(someStr.substring(s1, i1))version1, version2的整数段转化为数值,进行比较:若结果为大于或小于关系,直接返回结果;若结果为相等,进行小数部分的比较。
比较小数大小,只比高位即可,所以可以沿用这个循环,只需要继续用i1i2向后移动,同时赋坐标值给s1s2。这样转化数值函数中的小数段每次遍历就只有一个字符:str.substring(i1, i1),即str.charAt(i1)

Solution
public class Solution {
    public int compareVersion(String version1, String version2) {
    char[] v1 = version1.toCharArray();
    char[] v2 = version2.toCharArray();
    int i1 = 0,i2 = 0;
    int s1 = 0,s2 = 0;
    while(i1 < v1.length || i2 < v2.length){
        while(i1 < v1.length && v1[i1] != ".") i1++;
        while(i2 < v2.length && v2[i2] != ".") i2++;
        int a = s1 < v1.length ? Integer.valueOf(version1.substring(s1,i1)) : 0;
        int b = s2 < v2.length ? Integer.valueOf(version2.substring(s2,i2)) : 0;
        if(a > b) return 1;
        if(a < b) return -1;
        s1 = ++i1;
        s2 = ++i2;
    }
    return 0;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/64789.html

相关文章

  • [Leetcode] Compare Version Numbers 比较版本号

    摘要:注意因为方法输入的是一个正则表达式所以不能直接用,而是要用,而的要转义,所有要用代码按照进行分割比对相应的子串如果某个版本号更长,判断其多余部分是否是,如果不是,则较长的较大,否则是一样的。 Compare Version Numbers Compare two version numbers version1 and version2. If version1 > version2...

    FrozenMap 评论0 收藏0
  • [LeetCode] Compare Version Numbers

    Problem 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 an...

    Alex 评论0 收藏0
  • [LeetCode] 165. Compare Version Numbers

    Problem 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 an...

    赵春朋 评论0 收藏0
  • leetcode165. Compare Version Numbers

    摘要:题目要求也就是说,比较版本号。思路一利用通过方法将版本通过分隔开,然后将每一段版本从转化为进行比较思路二自己实现转化为自己实现将转化为,可以通过循环的方式。这是一个基本的算法。 题目要求 Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < ve...

    Mike617 评论0 收藏0
  • [LintCode/LeetCode] Find Median From / Data Stream

    摘要:建立两个堆,一个堆就是本身,也就是一个最小堆另一个要写一个,使之成为一个最大堆。我们把遍历过的数组元素对半分到两个堆里,更大的数放在最小堆,较小的数放在最大堆。同时,确保最大堆的比最小堆大,才能从最大堆的顶端返回。 Problem Numbers keep coming, return the median of numbers at every time a new number a...

    zxhaaa 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<