Push Dominoes Solutions in Go
Number 838
Difficulty Medium
Acceptance 48.6%
Link LeetCode
Other languages —
Solutions
Go solution by halfrost/LeetCode-Go
package leetcode// 解法一func pushDominoes(dominoes string) string {d := []byte(dominoes)for i := 0; i < len(d); {j := i + 1for j < len(d)-1 && d[j] == '.' {j++}push(d[i : j+1])i = j}return string(d)}func push(d []byte) {first, last := 0, len(d)-1switch d[first] {case '.', 'L':if d[last] == 'L' {for ; first < last; first++ {d[first] = 'L'}}case 'R':if d[last] == '.' || d[last] == 'R' {for ; first <= last; first++ {d[first] = 'R'}} else if d[last] == 'L' {for first < last {d[first] = 'R'd[last] = 'L'first++last--}}}}// 解法二func pushDominoes1(dominoes string) string {dominoes = "L" + dominoes + "R"res := ""for i, j := 0, 1; j < len(dominoes); j++ {if dominoes[j] == '.' {continue}if i > 0 {res += string(dominoes[i])}middle := j - i - 1if dominoes[i] == dominoes[j] {for k := 0; k < middle; k++ {res += string(dominoes[i])}} else if dominoes[i] == 'L' && dominoes[j] == 'R' {for k := 0; k < middle; k++ {res += string('.')}} else {for k := 0; k < middle/2; k++ {res += string('R')}for k := 0; k < middle%2; k++ {res += string('.')}for k := 0; k < middle/2; k++ {res += string('L')}}i = j}return res}