|
1 | 1 | packagecom.fishercoder.solutions;
|
2 |
| -/** |
3 |
| - * 165. Compare Version Numbers |
4 |
| -
|
5 |
| - Compare two version numbers version1 and version2. |
6 |
| - If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. |
7 |
| -
|
8 |
| - You may assume that the version strings are non-empty and contain only digits and the . character. |
9 |
| - The . character does not represent a decimal point and is used to separate number sequences. |
10 |
| - For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision. |
11 |
| -
|
12 |
| - Here is an example of version numbers ordering: |
13 |
| -
|
14 |
| - 0.1 < 1.1 < 1.2 < 13.37 |
15 |
| - */ |
16 | 2 |
|
17 | 3 | publicclass_165 {
|
18 |
| -publicstaticclassSolution1 { |
19 |
| -publicintcompareVersion(Stringversion1,Stringversion2) { |
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 |
| -intlen = (v1s.length <v2s.length) ?v2s.length :v1s.length; |
24 |
| -for (inti =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 |
| - }elseif (v2s.length ==i) { |
33 |
| -while (i <len) { |
34 |
| -if (Integer.parseInt(v1s[i]) >0) { |
35 |
| -return1; |
| 4 | +publicstaticclassSolution1 { |
| 5 | +publicintcompareVersion(Stringversion1,Stringversion2) { |
| 6 | +String[]v1s =version1.split( |
| 7 | +"\\.");//escaping it is very important! Otherwise, it's not going to work as expected! |
| 8 | +String[]v2s =version2.split("\\."); |
| 9 | +intlen = (v1s.length <v2s.length) ?v2s.length :v1s.length; |
| 10 | +for (inti =0;i <len;i++) { |
| 11 | +if (v1s.length ==i) { |
| 12 | +while (i <len) { |
| 13 | +if (Integer.parseInt(v2s[i]) >0) { |
| 14 | +return -1; |
| 15 | + } |
| 16 | +i++; |
| 17 | + } |
| 18 | + }elseif (v2s.length ==i) { |
| 19 | +while (i <len) { |
| 20 | +if (Integer.parseInt(v1s[i]) >0) { |
| 21 | +return1; |
| 22 | + } |
| 23 | +i++; |
| 24 | + } |
| 25 | + }else { |
| 26 | +if (Integer.parseInt(v1s[i]) >Integer.parseInt(v2s[i])) { |
| 27 | +return1; |
| 28 | + }elseif (Integer.parseInt(v2s[i]) >Integer.parseInt(v1s[i])) { |
| 29 | +return -1; |
| 30 | + } |
| 31 | + } |
36 | 32 | }
|
37 |
| -i++; |
38 |
| - } |
39 |
| - }else { |
40 |
| -if (Integer.parseInt(v1s[i]) >Integer.parseInt(v2s[i])) { |
41 |
| -return1; |
42 |
| - }elseif (Integer.parseInt(v2s[i]) >Integer.parseInt(v1s[i])) { |
43 |
| -return -1; |
44 |
| - } |
| 33 | +return0; |
45 | 34 | }
|
46 |
| - } |
47 |
| -return0; |
48 | 35 | }
|
49 |
| - } |
50 | 36 | }
|