望着时间滴答滴答的流过,我不曾改变过 . . .

leetcode 165. Compare Version Numbers

注意点:
java split 分割'.' 要进行转义

public int compareVersion(String version1, String version2) {
        String[] sv1 = version1.split("\\.");
        String[] sv2 = version2.split("\\.");
        int[] iv1 = new int[sv1.length];
        int[] iv2 = new int[sv2.length];
        
        for (int i = 0; i < sv1.length; i++)
            iv1[i] = Integer.parseInt(sv1[i]);
        for (int i = 0; i< sv2.length; i++)
            iv2[i] = Integer.parseInt(sv2[i]);
        
        int i = 0, j = 0;
        while (i < iv1.length && j < iv2.length)
            if (iv1[i] < iv2[j])
                return -1;        
            else if (iv1[i] > iv2[j])
                return 1;
            else{
                i++; j++;
            }
        
        while (i < iv1.length)
            if (iv1[i] != 0)
                return 1;
            else
                i++;
        while (j < iv2.length)
            if (iv2[j] != 0)
                return -1;
            else
                j++;
        return 0;
    }
posted @ 2019-04-09 14:25  whyaza  阅读(111)  评论(0编辑  收藏  举报