Closed congr closed 5 years ago
class Solution {
public int compareVersion(String version1, String version2) {
String[] v1 = version1.split("\\."); // !!! [ ] or \\
String[] v2 = version2.split("\\."); // !!! [ ] or \\
int p1 = 0, p2 = 0;
while (p1 < v1.length || p2 < v2.length) { // 1.0.1 vs 1.0 => 1
Integer n1 = (p1 < v1.length) ? Integer.parseInt(v1[p1]) : 0; // !!!
Integer n2 = (p2 < v2.length) ? Integer.parseInt(v2[p2]) : 0; // !!!
if (n1.compareTo(n2) != 0) return n1.compareTo(n2);
p1++;p2++;
}
return 0;
}
}
https://leetcode.com/problems/compare-version-numbers/