forked from yuzhangcmu/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareVersion2.java
More file actions
executable file
·52 lines (41 loc) · 1.32 KB
/
CompareVersion2.java
File metadata and controls
executable file
·52 lines (41 loc) · 1.32 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package Algorithms.string;
public class CompareVersion2 {
public static void main(String[] args) {
System.out.println(compareVersion("01", "1.0.1"));
}
public static int compareVersion(String version1, String version2) {
if (version1 == null || version2 == null) {
return 0;
}
int len1 = version1.length();
int len2 = version2.length();
int p1 = 0;
int p2 = 0;
int v1 = 0;
int v2 = 0;
while (true) {
v1 = 0;
v2 = 0;
// Like this: 1.3 VS 1.3.4
// The length of the two version are not equal.
if (p1 >= len1 && p2 >= len2) {
return 0;
}
while (p1 < len1 && version1.charAt(p1) != '.') {
v1 = v1 * 10 + ((int)version1.charAt(p1) - '0');
p1++;
}
while (p2 < len2 && version2.charAt(p2) != '.') {
v2 = v2 * 10 + ((int)version2.charAt(p2) - '0');
p2++;
}
if (v1 > v2) {
return 1;
} else if (v1 < v2) {
return -1;
}
p1++;
p2++;
}
}
}