zoukankan      html  css  js  c++  java
  • LeetCode 838. 推多米诺

    一行中有 N 张多米诺骨牌,我们将每张多米诺骨牌垂直竖立。
    在开始时,我们同时把一些多米诺骨牌向左或向右推。
    每过一秒,倒向左边的多米诺骨牌会推动其左侧相邻的多米诺骨牌。
    同样地,倒向右边的多米诺骨牌也会推动竖立在其右侧的相邻多米诺骨牌。
    如果同时有多米诺骨牌落在一张垂直竖立的多米诺骨牌的两边,由于受力平衡, 该骨牌仍然保持不变。
    就这个问题而言,我们会认为正在下降的多米诺骨牌不会对其它正在下降或已经下降的多米诺骨牌施加额外的力。
    给定表示初始状态的字符串 "S" 。如果第 i 张多米诺骨牌被推向左边,则 S[i] = 'L';如果第 i 张多米诺骨牌被推向右边,则 S[i] = 'R';如果第 i 张多米诺骨牌没有被推动,则 S[i] = '.'。
    返回表示最终状态的字符串。

    示例 1:
    输入:".L.R...LR..L.."
    输出:"LL.RR.LLRRLL.."

    示例 2:
    输入:"RR.L"
    输出:"RR.L"
    说明:第一张多米诺骨牌没有给第二张施加额外的力。
    提示:

    0 <= N <= 10^5
    表示多米诺骨牌状态的字符串只含有 'L','R'; 以及 '.';

    class Solution:
        def pushDominoes(self, dominoes: str) -> str:
            if len(dominoes)<2:
                return dominoes
            last_ans = dominoes
            ans = None
            while 1:
                ans_list = []
                for i in range(len(last_ans)):
                    if i==0:
                        pre = '.'
                        cur = last_ans[i]
                        cur_next = last_ans[i+1]
                    elif i==len(last_ans)-1:
                        pre = last_ans[i-1]
                        cur = last_ans[i]
                        cur_next = '.'
                    else:
                        pre = last_ans[i-1]
                        cur = last_ans[i]
                        cur_next = last_ans[i+1]
                    if cur !='.':
                        ans_list.append(cur)
                    else:
                        if pre == 'R' and cur_next == 'L':
                            ans_list.append(cur)
                        elif pre == 'R':
                            ans_list.append('R')
                        elif cur_next == 'L':
                            ans_list.append('L') 
                        else:
                            ans_list.append('.')
                ans =  ''.join(ans_list)
                #print(ans)
                if ans == last_ans:
                    break
                else:
                    last_ans = ans
            return ans
    
  • 相关阅读:
    java获取当前时间(年~~毫秒)
    python获取当前时间
    PyThon3运算符
    PyThon3中判断一个内容属于另一个内容("a" 在 “abcd”中
    PyThon3判断语句
    【BZOJ2460】元素(BJOI2011)-异或线性基+贪心
    【BZOJ3534】重建(SDOI2014)-矩阵树定理
    【BZOJ1227】虔诚的墓主人(SDOI2009)-线段树+离散化+组合数
    【BZOJ2815】灾难(ZJOI2012)-拓扑排序+建树+LCA
    【BZOJ2209】括号序列(JSOI2011)-Splay
  • 原文地址:https://www.cnblogs.com/sandy-t/p/13289333.html
Copyright © 2011-2022 走看看