class Solution(object):
def pushDominoes(self, dominoes):
symbols = [(i, x) for i, x in enumerate(dominoes) if x != '.']
symbols = [(-1, 'L')] + symbols + [(len(dominoes), 'R')]
ans = list(dominoes)
for (i, x), (j, y) in zip(symbols, symbols[1:]):
if x == y:
for k in range(i + 1, j):
ans[k] = x
# because R>L,and this is the solution for the case that like R...L
# if L...R,there is no need to change anything,so just ignore.
elif x > y:
for k in range(i + 1, j):
ans[k] = '.LR'[cmp(k - i, j - k)]
return "".join(ans)
def cmp(a, b):
if a == b:
return 0
elif a > b:
return 1
else:
return 2
'''
输入:".L.R...LR..L.."
输出:"LL.RR.LLRRLL.."
输入:"RR.L"
输出:"RR.L"
说明:第一张多米诺骨牌没有给第二张施加额外的力。
'''
print(Solution.pushDominoes(Solution, ".L.R...LR..L.."))
多米诺骨牌
最新推荐文章于 2021-01-16 14:54:48 发布