Skip to content

Commit 9f60104

Browse files
committed
165 Compare Version Numbers
1 parent ac51876 commit 9f60104

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
+ [152 Maximum Product Subarray(DP)](algorithms/MaximumProductSubarray)
3939
+ [153 Find Minimum in Rotated Sorted Array](algorithms/FindMinimuminRotatedSortedArray)
4040
+ [160 Intersection of Two Linked Lists](algorithms/IntersectionofTwoLinkedLists)
41+
+ [165 Compare Version Numbers](algorithms/CompareVersionNumbers)
4142
+ [169 Majority Element](algorithms/MajorityElement)
4243
+ [179 Largest Number](algorithms/LargestNumber)
4344
+ [189 Rotate Array](algorithms/RotateArray)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## Compare Version Numbers
2+
3+
Compare two version numbers version1 and version2.
4+
If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.
5+
6+
You may assume that the version strings are non-empty and contain only digits and the . character.
7+
The . character does not represent a decimal point and is used to separate number sequences.
8+
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.
9+
10+
Here is an example of version numbers ordering:
11+
12+
`0.1 < 1.1 < 1.2 < 13.37`
13+
Credits:
14+
Special thanks to @ts for adding this problem and creating all test cases.
15+
16+
## Sulution
17+
18+
直接按`.`划分,依次比较即可,如果比较不相等后面就不需要比较了。比如`1.2.12 和 1.3.123`,当第二个子版本序列`2 < 3`,因此后面不需要比较了。
19+
20+
注意不能用`sprintf(buf, "%f", version)`把字符串转化成浮点再比较大小,因为有可能有多个`.`,即多个子版本。
21+
22+
## Code
23+
```c
24+
int compareVersion(char* version1, char* version2) {
25+
char *p = version1;
26+
char *q = version2;
27+
while (*p || *q) {
28+
int d1 = 0;
29+
while (*p && isdigit(*p)) {
30+
d1 *= 10;
31+
d1 += (*p - '0');
32+
p++;
33+
}
34+
int d2 = 0;
35+
while (*q && isdigit(*q)) {
36+
d2 *= 10;
37+
d2 += (*q - '0');
38+
q++;
39+
}
40+
if (d1 > d2)
41+
return 1;
42+
if (d1 < d2)
43+
return -1;
44+
if (*p)
45+
p++;
46+
if (*q)
47+
q++;
48+
}
49+
return 0;
50+
}
51+
```
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1.1.1 1.1.2
2+
1.2.2 1.1.1
3+
1.1 2.1
4+
1 1.2
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include <ctype.h>
5+
int compareVersion(char* version1, char* version2) {
6+
char *p = version1;
7+
char *q = version2;
8+
while (*p || *q) {
9+
int d1 = 0;
10+
while (*p && isdigit(*p)) {
11+
d1 *= 10;
12+
d1 += (*p - '0');
13+
p++;
14+
}
15+
int d2 = 0;
16+
while (*q && isdigit(*q)) {
17+
d2 *= 10;
18+
d2 += (*q - '0');
19+
q++;
20+
}
21+
if (d1 > d2)
22+
return 1;
23+
if (d1 < d2)
24+
return -1;
25+
if (*p)
26+
p++;
27+
if (*q)
28+
q++;
29+
}
30+
return 0;
31+
}
32+
int main(int argc, char **argv)
33+
{
34+
char s1[20],s2[20];
35+
while (scanf("%s%s", s1, s2) != EOF) {
36+
switch (compareVersion(s1, s2)) {
37+
case 1:
38+
printf("%s > %s\n", s1, s2);
39+
break;
40+
case -1:
41+
printf("%s < %s\n", s1, s2);
42+
break;
43+
default:
44+
printf("%s == %s\n", s1, s2);
45+
}
46+
}
47+
return 0;
48+
}

0 commit comments

Comments
 (0)