题目:165. 比较版本号
思路:用正则表达式把版本号分开后挨个比较即可。
代码:
class Solution {
public int compareVersion(String version1, String version2) {
String[] array1 = version1.split("\\.");
String[] array2 = version2.split("\\.");
int n1 = array1.length;
int n2 = array2.length;
int n = Math.max(n1, n2);
for (int i = 0; i < n; i ++) {
int a = i < n1? Integer.parseInt(array1[i]): 0;
int b = i < n2? Integer.parseInt(array2[i]): 0;
if (a != b) {
return a > b? 1 : -1;
}
}
return 0;
}
}