|
1 | 1 | package com.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 | public class _165 {
|
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; |
| 4 | + public static class Solution1 { |
| 5 | + public int compareVersion(String version1, String version2) { |
| 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 | + int len = (v1s.length < v2s.length) ? v2s.length : v1s.length; |
| 10 | + for (int i = 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 | + } else if (v2s.length == i) { |
| 19 | + while (i < len) { |
| 20 | + if (Integer.parseInt(v1s[i]) > 0) { |
| 21 | + return 1; |
| 22 | + } |
| 23 | + i++; |
| 24 | + } |
| 25 | + } else { |
| 26 | + if (Integer.parseInt(v1s[i]) > Integer.parseInt(v2s[i])) { |
| 27 | + return 1; |
| 28 | + } else if (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 |
| - return 1; |
42 |
| - } else if (Integer.parseInt(v2s[i]) > Integer.parseInt(v1s[i])) { |
43 |
| - return -1; |
44 |
| - } |
| 33 | + return 0; |
45 | 34 | }
|
46 |
| - } |
47 |
| - return 0; |
48 | 35 | }
|
49 |
| - } |
50 | 36 | }
|
0 commit comments