-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPushDominoes.java
46 lines (45 loc) · 1.31 KB
/
PushDominoes.java
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
/*https://leetcode.com/problems/push-dominoes/*/
class Solution {
public String pushDominoes(String dominoes) {
char[] d = dominoes.toCharArray();
int n = d.length, prevDomino = -1;
for (int i = 0; i < n; ++i)
{
char c = d[i];
if (c == 'L')
{
if (prevDomino == -1 || d[prevDomino] == 'L')
{
for (int j = i-1; j > prevDomino; --j)
d[j] = 'L';
}
else
{
int left = prevDomino+1;
int right = i-1;
while (left < right)
{
d[left++] = 'R';
d[right--] = 'L';
}
}
prevDomino = i;
}
else if (c == 'R')
{
if (prevDomino != -1 && d[prevDomino] == 'R')
{
for (int j = i-1; j > prevDomino; --j)
d[j] = 'R';
}
prevDomino = i;
}
}
if (prevDomino != -1 && d[prevDomino] == 'R')
{
for (int j = n-1; j > prevDomino; --j)
d[j] = 'R';
}
return new String(d);
}
}