-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path838.cpp
More file actions
52 lines (52 loc) · 1.02 KB
/
Copy path838.cpp
File metadata and controls
52 lines (52 loc) · 1.02 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
class Solution {
public:
string pushDominoes(string dominoes) {
int length = dominoes.length();
string ret;
int start = 0;
for (int i = 0; i <= length; ++i) { //".L.R."
if (dominoes[i] == 'L') {
for (int j = start; j <= i; ++j)
ret.push_back('L');
start = i + 1;
}
else if (dominoes[i] == 'R') {
ret.append(i - start, '.');
int j = i + 1;
while (j <= length) {
if (j == length) {
ret.append(j - i, 'R');
i = j + 1;
break;
}
else if (dominoes[j] == 'L') {
int l = j - i;
if (l % 2 == 0) {
ret.append(l / 2, 'R');
ret.push_back('.');
ret.append(l / 2, 'L');
}
else {
ret.append(l / 2 + 1, 'R');
ret.append(l / 2 + 1, 'L');
}
i = j;
start = j + 1;
break;
}
else if (dominoes[j] == 'R')
{
ret.append(j - i, 'R');
i = j - 1;
start = j;
break;
}
++j;
}
}
if (i == length)
ret.append(i - start, '.');
}
return ret;
}
};