美文网首页
165. Compare Version Numbers

165. Compare Version Numbers

作者: 7ccc099f4608 | 来源:发表于2020-03-18 22:29 被阅读0次

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;
    }

相关文章

网友评论

      本文标题:165. Compare Version Numbers

      本文链接:https://www.haomeiwen.com/subject/flshyhtx.html