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 b0907da

Browse files
AC 838
1 parent 3d4d77b commit b0907da

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (C) 2022, Saul Lawliet <october dot sunbathe at gmail dot com>
3+
* All rights reserved.
4+
*
5+
* 两个指针, 模拟实际操作过程即可
6+
* 第一次提交的时候, 没有这个情况: ".L.R."
7+
*/
8+
9+
#include <stdbool.h>
10+
#include "c/test.h"
11+
12+
char *pushDominoes(char *dominoes) {
13+
int slow = 0, fast = 0;
14+
15+
for (; dominoes[fast] != '0円'; fast++) {
16+
if (dominoes[fast] == 'L') {
17+
if (dominoes[slow] == 'L' || dominoes[slow] == '.') {
18+
// all set 'L'
19+
while (slow < fast) dominoes[slow++] = 'L';
20+
} else {
21+
// 此时是R, 向中间靠拢
22+
int mid = (fast + slow) / 2;
23+
bool even = (fast + slow) % 2 == 0;
24+
while (slow < mid) dominoes[slow++] = 'R';
25+
26+
if (even)
27+
slow++;
28+
else
29+
dominoes[slow++] = 'R';
30+
31+
while (slow < fast) dominoes[slow++] = 'L';
32+
}
33+
} else if (dominoes[fast] == 'R') {
34+
if (dominoes[slow] == 'L' || dominoes[slow] == '.') {
35+
// 忽略
36+
slow = fast;
37+
} else {
38+
// all set 'R'
39+
while (slow < fast) dominoes[slow++] = 'R';
40+
}
41+
}
42+
}
43+
44+
// 注意, 如何最后一次操作是‘R’, 要把后面都推倒
45+
if (dominoes[slow] == 'R') {
46+
while (slow < fast) dominoes[slow++] = 'R';
47+
}
48+
49+
return dominoes;
50+
}
51+
52+
void test(const char *expect, const char *dominoes) {
53+
int len = strlen(dominoes) + 1;
54+
char *str = malloc(sizeof(char) * len);
55+
memcpy(str, dominoes, len);
56+
57+
EXPECT_EQ_STRING_AND_FREE_ACTUAL(expect, pushDominoes(str));
58+
}
59+
60+
int main(void) {
61+
test("RR.L", "RR.L");
62+
test("LL.RR.LLRRLL..", ".L.R...LR..L..");
63+
64+
test("LL.RR", ".L.R.");
65+
66+
return testOutput();
67+
}

0 commit comments

Comments
(0)

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