Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathCompare Version Numbers.java
More file actions
Latest commit
executable file
·130 lines (116 loc) · 4.27 KB
/
Copy pathCompare Version Numbers.java
File metadata and controls
executable file
·130 lines (116 loc) · 4.27 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
M
1521777505
tags: String
给两串versionnumber, 由数字和'.'组成. 比较先后顺序.
Ifversion1 > version2return1, ifversion1 < version2return -1, otherwisereturn0.
#### divideandconquer
- 用str.split("\\.") 分割string
- Convert成integer, 逐个击破
#### 注意
- '1.0'和'0'是相等的
- 如果可以假设versioninteger都是valid, 直接Integer.parseInt()就可以了
- 不然的话, 可以comparestring
```
/*
Compare two version numbers version1 and version2.
If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.
You may assume that the version strings are non-empty and contain only digits and the . character.
The . character does not represent a decimal point and is used to separate number sequences.
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.
Here is an example of version numbers ordering:
0.1 < 1.1 < 1.2 < 13.37
*/
/*
Thoughts:
- divide and conqure: divide by '.'
- during the same cun, the one with no sub-division, it's the leading version
- use Integer.parseInt() with assumption that the numbers are valid integer.
*/
classSolution {
publicintcompareVersion(Stringversion1, Stringversion2) {
if (version1.equals(version2)) {
return0;
}
String[] subVersion1 = version1.split("\\.");
String[] subVersion2 = version2.split("\\.");
intsize = Math.max(subVersion1.length, subVersion2.length);
intrst = 0;
for (inti = 0; i < size; i++) {
if (i >= subVersion1.length) {
rst = Integer.parseInt(subVersion2[i]) == 0 ? 0 : -1; // assume the missing subVersion1 = 0
} elseif (i >= subVersion2.length) {
rst = Integer.parseInt(subVersion1[i]) == 0 ? 0 : 1; // assume the missing subVersion2 = 0
} else { // both exist
if (Integer.parseInt(subVersion1[i]) != Integer.parseInt(subVersion2[i])) {
rst = Integer.parseInt(subVersion1[i]) < Integer.parseInt(subVersion2[i]) ? -1 : 1;
}
}
if (rst != 0) {
returnrst;
}
}
returnrst;
}
}
/*
Thoughts:
- divide and conqure: divide by '.'
- during the same cun, the one with no sub-division, it's the leading version
- compare string
*/
classSolution {
publicintcompareVersion(Stringversion1, Stringversion2) {
if (version1.equals(version2)) {
return0;
}
if (!version1.contains(".") && !version2.contains(".")) {
returncompareString(version1, version2);
}
String[] subVersion1 = version1.split("\\.");
String[] subVersion2 = version2.split("\\.");
intsize = Math.max(subVersion1.length, subVersion2.length);
intrst = 0;
for (inti = 0; i < size; i++) {
if (i >= subVersion1.length) {
rst = compareString("0", subVersion2[i]);
} elseif (i >= subVersion2.length) {
rst = compareString(subVersion1[i], "0");
} else {
rst = compareString(subVersion1[i], subVersion2[i]);
}
if (rst != 0) {
returnrst;
}
}
returnrst;
}
/*
Assume the number can be super large, and can't be saved in Integer, or Long.
Compare number by each digit
*/
privateintcompareString(Stringstr1, Stringstr2) {
if (str1.equals(str2)) {
return0;
}
while (str1 != null && str1.length() > 1 && str1.charAt(0) == '0') {
str1 = str1.substring(1);
}
while (str2 != null && str2.length() > 1 && str2.charAt(0) == '0') {
str2 = str2.substring(1);
}
if (str1.length() != str2.length()) {
returnstr1.length() < str2.length() ? -1 : 1;
}
for (inti = 0; i < str1.length(); i++) {
intdigit1 = str1.charAt(i) - '0';
intdigit2 = str2.charAt(i) - '0';
if (digit1 < digit2) {
return -1;
} elseif (digit1 > digit2) {
return1;
}
}
return0;
}
}
```