比较版本号

以.进行分割,逐一进行转义//. ,之后数字进行比较就ok了


class Solution {
    public int compareVersion(String version1, String version2) {

    String [] v1 = version1.split("\\.");//.用//进行转义
    String [] v2 = version2.split("\\.");
    int v1Len = v1.length,v2Len = v2.length;
    int i = 0;
    for(i=0;i<Math.min(v1Len,v2Len);i++){
        if(Integer.valueOf(v1[i]) > Integer.valueOf(v2[i])) return 1;
        if(Integer.valueOf(v1[i]) < Integer.valueOf(v2[i])) return -1;
    }
    while(i<v1Len){
        if(Integer.valueOf(v1[i]) > 0) return 1;
        i++;
    }
    while(i<v2Len){
        if(Integer.valueOf(v2[i]) > 0) return -1;
        i++;
    }
    return 0;

    }
}

posted @ 2020-07-29 21:55  浅滩浅  阅读(193)  评论(0编辑  收藏  举报