-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay34.java
More file actions
36 lines (28 loc) · 1.07 KB
/
Day34.java
File metadata and controls
36 lines (28 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class Day34 {
public static int compareVersion(String version1, String version2) {
String[] v1 = version1.split("\\.");
String[] v2 = version2.split("\\.");
int maxLength = Math.max(v1.length, v2.length);
for (int i = 0; i < maxLength; i++) {
int num1 = i < v1.length ? Integer.parseInt(v1[i]) : 0;
int num2 = i < v2.length ? Integer.parseInt(v2[i]) : 0;
if (num1 < num2) {
return -1;
} else if (num1 > num2) {
return 1;
}
}
return 0;
}
public static void main(String[] args) {
String version1 = "1.01";
String version2 = "1.001";
System.out.println(compareVersion(version1, version2)); // Output: 0
version1 = "1.0";
version2 = "1.0.0";
System.out.println(compareVersion(version1, version2)); // Output: 0
version1 = "0.1";
version2 = "1.1";
System.out.println(compareVersion(version1, version2)); // Output: -1
}
}