165165165

165

class Solution {
    int res = 0;
    int flag = 1;
    String[] versionlong;
    String[] versionshort;
    public int compareVersion(String version1, String version2) {
        String[] version1_sep = version1.split("\\.");
        String[] version2_sep = version2.split("\\.");
        int index = Math.min(version1_sep.length, version2_sep.length);
        int lenMax = Math.max(version1_sep.length, version2_sep.length);
        swap(version1, version2, version1_sep, version2_sep);
        int i;
        for(i = 0; i < index; i++){
            res = (Integer.parseInt(versionlong[i]) - Integer.parseInt(versionshort[i]));
            // System.out.println("index:" + i 
            //                  + " | version1:" + version1_sep[i]
            //                  + " | version2:" + version2_sep[i]
            //                  + " | res:" + res);
            if(res < 0){
                res = -1;
                break;
            } else if(res > 0){
                res = 1;
                break;
            }
        }
        if(res == 0){
            for(; i < lenMax; i++){
                if(Integer.parseInt(versionlong[i]) > 0){
                    res = 1;
                    break;
                }
            }
        }
        return res*flag;
    }
    //由于version1与version2的大小比较存在顺序,因此顺序颠倒后需要记录
    public void swap(String version1, String version2, String[] version1_sep, String[] version2_sep){
        if(version1.length() >= version2.length()){
            versionlong = version1_sep;
            versionshort = version2_sep;
            flag = 1;
        } else {
            versionlong = version2_sep;
            versionshort = version1_sep;
            flag = -1;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值