Saturday, October 7, 2017
165. Compare Version Numbers
11.57 %
class Solution {
public int compareVersion(String version1, String version2) {
if (version1 == null || version2 == null) return 0;
String[] array1 = version1.split("\\.");
String[] array2 = version2.split("\\.");
int i = 0;
for (i = 0; i < array1.length || i < array2.length; i++) {
Integer v1 = i < array1.length ? Integer.valueOf(array1[i]) : 0;
Integer v2 = i < array2.length ? Integer.valueOf(array2[i]) : 0;
int compare = v1.compareTo(v2);
if (compare != 0) return compare;
}
return 0;
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment