-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy path0838-push-dominoes.java
More file actions
28 lines (27 loc) · 901 Bytes
/
0838-push-dominoes.java
File metadata and controls
28 lines (27 loc) · 901 Bytes
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
class Solution {
public String pushDominoes(String dominoes) {
int len = dominoes.length();
Queue<Integer> q = new LinkedList<>();
char[] dom = dominoes.toCharArray();
for (int i = 0; i < len; i++)
if (dominoes.charAt(i) != '.') q.offer(i);
while (!q.isEmpty()) {
int i = q.poll();
char ch = dom[i];
if (dom[i] == 'R') {
if (i + 1 < len && dom[i + 1] == '.') {
if (i + 2 < len && dom[i + 2] == 'L') {
q.poll();
} else {
dom[i + 1] = 'R';
q.offer(i + 1);
}
}
} else if (i > 0 && dom[i - 1] == '.') {
dom[i - 1] = 'L';
q.offer(i - 1);
}
}
return String.valueOf(dom);
}
}