其他分享
首页 > 其他分享> > 165.CompareVersionNumbers

165.CompareVersionNumbers

作者:互联网

Input: version1 = "0.1", version2 = "1.1" Output: -1
 
Input: version1 = "7.5.2.4", version2 = "7.5.3" Output: -1

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

标签:version1,version2,int,levels1,CompareVersionNumbers,length,Integer,165
来源: https://www.cnblogs.com/MarkLeeBYR/p/10677863.html