165. Compare Version Numbers

2020-03-18  本文已影响0人  7ccc099f4608

https://leetcode-cn.com/problems/compare-version-numbers/

image.png

(图片来源https://leetcode-cn.com/problems/compare-version-numbers/

日期 是否一次通过 comment
2020-03-18 0
2020-03-18 0

public int compareVersion(String version1, String version2) {
        String[] levels1 = version1.split("\\.");
        String[] levels2 = version2.split("\\.");
        
        int length = Math.max(levels1.length, levels2.length);
        for (int i=0; i<length; i++) {
            Integer v1 = i < levels1.length ? Integer.parseInt(levels1[i]) : 0;
            Integer v2 = i < levels2.length ? Integer.parseInt(levels2[i]) : 0;
            int compare = v1.compareTo(v2);
            if (compare != 0) {
                return compare;
            }
        }
        
        return 0;
    }
上一篇 下一篇

猜你喜欢

热点阅读