|
1 | 1 | 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. |
3 | 6 | If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.
|
4 | 7 |
|
5 | 8 | You may assume that the version strings are non-empty and contain only digits and the . character.
|
|
8 | 11 |
|
9 | 12 | Here is an example of version numbers ordering:
|
10 | 13 |
|
11 |
| - 0.1 < 1.1 < 1.2 < 13.37*/ |
| 14 | + 0.1 < 1.1 < 1.2 < 13.37 |
| 15 | + */ |
12 | 16 |
|
13 | 17 | 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; |
40 | 36 | }
|
| 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 | + } |
41 | 45 | }
|
42 |
| - return 0; |
| 46 | + } |
| 47 | + return 0; |
43 | 48 | }
|
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 | + } |
59 | 50 | }
|
0 commit comments