Skip to content

Commit f0791c0

Browse files
refactor 165
1 parent 3887461 commit f0791c0

File tree

2 files changed

+66
-44
lines changed

2 files changed

+66
-44
lines changed
Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.fishercoder.solutions;
2-
/**Compare two version numbers version1 and version2.
2+
/**
3+
* 165. Compare Version Numbers
4+
5+
Compare two version numbers version1 and version2.
36
If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.
47
58
You may assume that the version strings are non-empty and contain only digits and the . character.
@@ -8,52 +11,40 @@
811
912
Here is an example of version numbers ordering:
1013
11-
0.1 < 1.1 < 1.2 < 13.37*/
14+
0.1 < 1.1 < 1.2 < 13.37
15+
*/
1216

1317
public class _165 {
14-
15-
public static int compareVersion(String version1, String version2) {
16-
String[] v1s = version1.split("\\.");//escaping it is very important! Otherwise, it's not going to work as expected!
17-
String[] v2s = version2.split("\\.");
18-
int len = (v1s.length < v2s.length) ? v2s.length : v1s.length;
19-
for (int i = 0; i < len; i++) {
20-
if (v1s.length == i) {
21-
while (i < len) {
22-
if (Integer.parseInt(v2s[i]) > 0) {
23-
return -1;
24-
}
25-
i++;
26-
}
27-
} else if (v2s.length == i) {
28-
while (i < len) {
29-
if (Integer.parseInt(v1s[i]) > 0) {
30-
return 1;
31-
}
32-
i++;
33-
}
34-
} else {
35-
if (Integer.parseInt(v1s[i]) > Integer.parseInt(v2s[i])) {
36-
return 1;
37-
} else if (Integer.parseInt(v2s[i]) > Integer.parseInt(v1s[i])) {
38-
return -1;
39-
}
18+
public static class Solution1 {
19+
public int compareVersion(String version1, String version2) {
20+
String[] v1s = version1.split(
21+
"\\.");//escaping it is very important! Otherwise, it's not going to work as expected!
22+
String[] v2s = version2.split("\\.");
23+
int len = (v1s.length < v2s.length) ? v2s.length : v1s.length;
24+
for (int i = 0; i < len; i++) {
25+
if (v1s.length == i) {
26+
while (i < len) {
27+
if (Integer.parseInt(v2s[i]) > 0) {
28+
return -1;
29+
}
30+
i++;
31+
}
32+
} else if (v2s.length == i) {
33+
while (i < len) {
34+
if (Integer.parseInt(v1s[i]) > 0) {
35+
return 1;
4036
}
37+
i++;
38+
}
39+
} else {
40+
if (Integer.parseInt(v1s[i]) > Integer.parseInt(v2s[i])) {
41+
return 1;
42+
} else if (Integer.parseInt(v2s[i]) > Integer.parseInt(v1s[i])) {
43+
return -1;
44+
}
4145
}
42-
return 0;
46+
}
47+
return 0;
4348
}
44-
45-
public static void main(String... args) {
46-
// String version1 = "1.1";
47-
// String version2 = "1.2";//should return -1
48-
49-
// String version1 = "1.0.1";
50-
// String version2 = "1";//should return 1
51-
52-
String version1 = "1.0";
53-
String version2 = "1";//should return 0
54-
55-
/**"1.0.1", "1"*/
56-
System.out.println(compareVersion(version1, version2));
57-
}
58-
49+
}
5950
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._165;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _165Test {
10+
private static _165.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _165.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(-1, solution1.compareVersion("1.1", "1.2"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(1, solution1.compareVersion("1.0.1", "1"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(-0, solution1.compareVersion("1.0", "1"));
30+
}
31+
}

0 commit comments

Comments
 (0)