Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 91b548a

Browse files
AC165
1 parent b0907da commit 91b548a

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

‎c/100-199/165-compare-version-numbers.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2023, Saul Lawliet <october dot sunbathe at gmail dot com>
3+
* All rights reserved.
4+
*
5+
* 最开始想用字符串分割来弄, 但 c 处理起来比较麻烦, 还是朴素方法好写一点.
6+
* 第一次提交的时候, 没有考虑 int 会越界, 后改用 int64_t 就可以了.
7+
*/
8+
9+
#include "c/test.h"
10+
11+
int compareVersion(char *version1, char *version2) {
12+
int64_t i1, i2;
13+
while (*version1 || *version2) {
14+
i1 = 0;
15+
while (*version1 && *version1 != '.') {
16+
i1 = i1 * 10 + *version1 - '0';
17+
version1++;
18+
}
19+
if (*version1 == '.') version1++;
20+
21+
i2 = 0;
22+
while (*version2 && *version2 != '.') {
23+
i2 = i2 * 10 + *version2 - '0';
24+
version2++;
25+
}
26+
if (*version2 == '.') version2++;
27+
28+
if (i1 != i2) return i1 < i2 ? -1 : 1;
29+
}
30+
31+
return 0;
32+
}
33+
34+
void test(int expect, char *version1, char *version2) {
35+
EXPECT_EQ_INT(expect, compareVersion(version1, version2));
36+
}
37+
38+
int main(void) {
39+
test(0, "1.01", "1.001");
40+
test(0, "1.0", "1.0.0");
41+
test(-1, "0.1", "1.1");
42+
43+
return testOutput();
44+
}

‎c/800-899/838-push-dominoes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* All rights reserved.
44
*
55
* 两个指针, 模拟实际操作过程即可
6-
* 第一次提交的时候, 没有这个情况: ".L.R."
6+
* 第一次提交的时候, 没有考虑到这个情况: ".L.R."
77
*/
88

99
#include <stdbool.h>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /