classSolution: defpushDominoes(self, dominoes: str) -> str: n = len(dominoes) q = deque() time = [-1] * n force = [[] for _ inrange(n)] for i, f inenumerate(dominoes): if f != '.': q.append(i) time[i] = 0 force[i].append(f)
res = ['.'] * n while q: i = q.popleft() iflen(force[i]) == 1: res[i] = f = force[i][0] ni = i - 1if f == 'L'else i + 1 if0 <= ni < n: t = time[i] if time[ni] == -1: q.append(ni) time[ni] = t + 1 force[ni].append(f) elif time[ni] == t + 1: force[ni].append(f) return''.join(res)
publicclassSolution { publicstringPushDominoes(string dominoes) { int n = dominoes.Length; Queue<int> queue = new Queue<int>(); int[] time = newint[n]; Array.Fill(time, -1); IList<char>[] force = new IList<char>[n]; for (int i = 0; i < n; i++) { force[i] = new List<char>(); } for (int i = 0; i < n; i++) { char f = dominoes[i]; if (f != '.') { queue.Enqueue(i); time[i] = 0; force[i].Add(f); } }
char[] res = newchar[n]; Array.Fill(res, '.'); while (queue.Count > 0) { int i = queue.Dequeue(); if (force[i].Count == 1) { char f = force[i][0]; res[i] = f; int ni = f == 'L' ? i - 1 : i + 1; if (ni >= 0 && ni < n) { int t = time[i]; if (time[ni] == -1) { queue.Enqueue(ni); time[ni] = t + 1; force[ni].Add(f); } elseif (time[ni] == t + 1) { force[ni].Add(f); } } } } returnnewstring(res); } }
classSolution { public: string pushDominoes(string dominoes){ int n = dominoes.size(); queue<int> q; vector<int> time(n, - 1); vector<string> force(n); for (int i = 0; i < n; i++) { if (dominoes[i] != '.') { q.emplace(i); time[i] = 0; force[i].push_back(dominoes[i]); } }
string res(n, '.'); while (!q.empty()) { int i = q.front(); q.pop(); if (force[i].size() == 1) { char f = force[i][0]; res[i] = f; int ni = (f == 'L') ? (i - 1) : (i + 1); if (ni >= 0and ni < n) { int t = time[i]; if (time[ni] == -1) { q.emplace(ni); time[ni] = t + 1; force[ni].push_back(f); } elseif(time[ni] == t + 1) { force[ni].push_back(f); } } } } return res; } };
intpopQueue(Queue * obj) { if (obj->length == 0) { return-1; } int res = obj->head->val; StListNode * node = obj->head; obj->head = obj->head->next; obj->length--; free(node); return res; }
char * pushDominoes(char * dominoes){ int n = strlen(dominoes); int * time = (int *)malloc(sizeof(int) * n); char * res = (char *)malloc(sizeof(char) * (n + 1)); Queue ** force = (Queue **)malloc(sizeof(Queue *) * n);
for (int i = 0; i < n; i++) { time[i] = -1; force[i] = (Queue *)malloc(sizeof(Queue)); initQueue(force[i]); res[i] = '.'; } res[n] = '\0'; Queue qu; initQueue(&qu); for (int i = 0; i < n; i++) { if (dominoes[i] != '.') { pushQueue(&qu, i); time[i] = 0; pushQueue(force[i], dominoes[i]); } }
while (!isEmpty(&qu)) { int i = popQueue(&qu); if (length(force[i]) == 1) { char f = front(force[i]); res[i] = f; int ni = (f == 'L') ? (i - 1) : (i + 1); if (ni >= 0 && ni < n) { int t = time[i]; if (time[ni] == -1) { pushQueue(&qu, ni); time[ni] = t + 1; pushQueue(force[ni], f); } elseif(time[ni] == t + 1) { pushQueue(force[ni], f); } } } } /* free resource */ for (int i = 0; i < n; i++) { while (!isEmpty(force[i])) { popQueue(force[i]); } } return res; }
funcpushDominoes(dominoes string)string { n := len(dominoes) q := []int{} time := make([]int, n) for i := range time { time[i] = -1 } force := make([][]byte, n) for i, ch := range dominoes { if ch != '.' { q = append(q, i) time[i] = 0 force[i] = append(force[i], byte(ch)) } }
ans := bytes.Repeat([]byte{'.'}, n) forlen(q) > 0 { i := q[0] q = q[1:] iflen(force[i]) > 1 { continue } f := force[i][0] ans[i] = f ni := i - 1 if f == 'R' { ni = i + 1 } if0 <= ni && ni < n { t := time[i] if time[ni] == -1 { q = append(q, ni) time[ni] = t + 1 force[ni] = append(force[ni], f) } elseif time[ni] == t+1 { force[ni] = append(force[ni], f) } } } returnstring(ans) }
classSolution: defpushDominoes(self, dominoes: str) -> str: s = list(dominoes) n, i, left = len(s), 0, 'L' while i < n: j = i while j < n and s[j] == '.': # 找到一段连续的没有被推动的骨牌 j += 1 right = s[j] if j < n else'R' if left == right: # 方向相同,那么这些竖立骨牌也会倒向同一方向 while i < j: s[i] = right i += 1 elif left == 'R'and right == 'L': # 方向相对,那么就从两侧向中间倒 k = j - 1 while i < k: s[i] = 'R' s[k] = 'L' i += 1 k -= 1 left = right i = j + 1 return''.join(s)
funcpushDominoes(dominoes string)string { s := []byte(dominoes) i, n, left := 0, len(s), byte('L') for i < n { j := i for j < n && s[j] == '.' { // 找到一段连续的没有被推动的骨牌 j++ } right := byte('R') if j < n { right = s[j] } if left == right { // 方向相同,那么这些竖立骨牌也会倒向同一方向 for i < j { s[i] = right i++ } } elseif left == 'R' && right == 'L' { // 方向相对,那么就从两侧向中间倒 k := j - 1 for i < k { s[i] = 'R' s[k] = 'L' i++ k-- } } left = right i = j + 1 } returnstring(s) }
var pushDominoes = function(dominoes) { const s = [...dominoes]; let n = s.length, i = 0; let left = 'L'; while (i < n) { let j = i; while (j < n && s[j] == '.') { // 找到一段连续的没有被推动的骨牌 j++; } const right = j < n ? s[j] : 'R'; if (left === right) { // 方向相同,那么这些竖立骨牌也会倒向同一方向 while (i < j) { s[i++] = right; } } elseif (left === 'R' && right === 'L') { // 方向相对,那么就从两侧向中间倒 let k = j - 1; while (i < k) { s[i++] = 'R'; s[k--] = 'L'; } } left = right; i = j + 1; } return s.join(''); };