forked from yuzhangcmu/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareVersion.java
More file actions
executable file
·35 lines (27 loc) · 951 Bytes
/
CompareVersion.java
File metadata and controls
executable file
·35 lines (27 loc) · 951 Bytes
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
package Algorithms.string;
public class CompareVersion {
public static void main(String[] args) {
compareVersion("1", "0");
}
public static int compareVersion(String version1, String version2) {
if (version1 == null || version2 == null || version1.length() == 0
|| version2.length() == 0) {
return 0;
}
String[] strs1 = version1.split("\\.");
String[] strs2 = version2.split("\\.");
int size1 = strs1.length;
int size2 = strs2.length;
int minSize = Math.min(size1, size2);
for (int i = 0; i < minSize; i++) {
int v1 = Integer.parseInt(strs1[i]);
int v2 = Integer.parseInt(strs2[i]);
if (v1 > v2) {
return 1;
} else if (v1 < v2) {
return -1;
}
}
return 0;
}
}