LeetCode 165. Compare Version Numbers(比较版本)

10 篇文章 0 订阅
10 篇文章 0 订阅

原题网址: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

方法一:使用String.split方法。

public class Solution {
    private int[] getVersion(String s) {
        String[] vs = s.split("\\.");
        int[] versions = new int[vs.length];
        for(int i=0; i<vs.length; i++) versions[i] = Integer.parseInt(vs[i]);
        return versions;
    }
    private int compare(int[] v1, int[] v2) {
        for(int i=0; i<Math.max(v1.length, v2.length); i++) {
            if (i<v1.length && i<v2.length) {
                if (v1[i] < v2[i]) return -1;
                if (v1[i] > v2[i]) return 1;
            } else if (i>=v1.length) {
                if (v2[i] > 0) return -1;
            } else {
                if (v1[i] > 0) return 1;
            }
        }
        return 0;
    }
    public int compareVersion(String version1, String version2) {
        int[] v1 = getVersion(version1);
        int[] v2 = getVersion(version2);
        return compare(v1, v2);
    }
}

方法二:手工解析。

public class Solution {
    private int getVersion(String s, int[] version) {
        int pos = 0;
        char[] sa = s.toCharArray();
        int val = 0;
        for(int i=0; i<sa.length; i++) {
            if (sa[i] == '.') {
                version[pos++] = val;
                val = 0;
            } else {
                val = val * 10 + (sa[i]-'0');
            }
        }
        version[pos++] = val;
        return pos;
    }
    public int compareVersion(String version1, String version2) {
        int[] nums1 = new int[version1.length()/2+2];
        int[] nums2 = new int[version2.length()/2+2];
        int len1 = getVersion(version1, nums1);
        int len2 = getVersion(version2, nums2);
        
        for(int i=0; i<Math.max(len1, len2); i++) {
            if (i<len1 && i<len2) {
                if (nums1[i] < nums2[i]) return -1;
                if (nums1[i] > nums2[i]) return 1;
            } else if (i<len1) {
                if (nums1[i] > 0) return 1;
            } else {
                if (nums2[i] > 0) return -1;
            }
        }
        return 0;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值