• [LeetCode] 838. Push Dominoes


    There are n dominoes in a line, and we place each domino vertically upright. In the beginning, we simultaneously push some of the dominoes either to the left or to the right.

    After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right.

    When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces.

    For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino.

    You are given a string dominoes representing the initial state where:

    • dominoes[i] = 'L', if the ith domino has been pushed to the left,
    • dominoes[i] = 'R', if the ith domino has been pushed to the right, and
    • dominoes[i] = '.', if the ith domino has not been pushed.

    Return a string representing the final state.

    Example 1:

    Input: dominoes = "RR.L"
    Output: "RR.L"
    Explanation: The first domino expends no additional force on the second domino.
    

    Example 2:

    Input: dominoes = ".L.R...LR..L.."
    Output: "LL.RR.LLRRLL.."

    Constraints:

    • n == dominoes.length
    • 1 <= n <= 105
    • dominoes[i] is either 'L''R', or '.'.

    推多米诺。

    n 张多米诺骨牌排成一行,将每张多米诺骨牌垂直竖立。在开始时,同时把一些多米诺骨牌向左或向右推。

    每过一秒,倒向左边的多米诺骨牌会推动其左侧相邻的多米诺骨牌。同样地,倒向右边的多米诺骨牌也会推动竖立在其右侧的相邻多米诺骨牌。

    如果一张垂直竖立的多米诺骨牌的两侧同时有多米诺骨牌倒下时,由于受力平衡, 该骨牌仍然保持不变。

    就这个问题而言,我们会认为一张正在倒下的多米诺骨牌不会对其它正在倒下或已经倒下的多米诺骨牌施加额外的力。

    给你一个字符串 dominoes 表示这一行多米诺骨牌的初始状态,其中:

    dominoes[i] = 'L',表示第 i 张多米诺骨牌被推向左侧,
    dominoes[i] = 'R',表示第 i 张多米诺骨牌被推向右侧,
    dominoes[i] = '.',表示没有推动第 i 张多米诺骨牌。
    返回表示最终状态的字符串。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/push-dominoes
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这是一道双指针的题目,思路不好想,但是想通了感觉挺有意思的一道题。我参考了这个帖子

    题意不难理解,给的字符串中只有L,R和点三种 input。这道题为什么可以用双指针是因为一个被推倒了的牌只对站着的牌起作用,换言之,只对一个字符串中是点的牌起作用,所以当牌按照 input 的方案被推倒的时候,实际只会出现如下几种情况。以下的点代表站立住的骨牌。

    'R......R' => 'RRRRRRRR'
    'R......L' => 'RRRRLLLL' or 'RRRR.LLLL'
    'L......R' => 'L......R'
    'L......L' => 'LLLLLLLL'

    作者:fuxuemingzhu
    链接:https://leetcode-cn.com/problems/push-dominoes/solution/fu-xue-ming-zhu-miao-dong-xi-lie-xiang-x-xkts/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    所以我们开始用双指针遍历 input 字符串,这里有一个特殊情况需要处理,因为双指针是一定需要有两个指针卡住一个区间的,所以我们在 input 字符串的最左边加了一个 L,在最右边加了一个 R,这样加不会影响题目的结果,但是这样可以形成一个区间。

    • 开始扫描,对于右指针来说,如果遇到点则暂时跳过
    • 当左右指针是左边 L 右边 R 的话,则 L 和 R 之间的内容原封不动地放入结果集,因为两个骨牌倒下的方向是相反的,类似这样,\\\\\\\\\\//////////
    • 当左右指针是左边 R 右边 L 的话,分两种情况,如果两者之间的距离是奇数的话,那么最中间那个位置的骨牌是不会倒下的,因为他的左边和右边的力量抵消了;如果两者之间的距离是偶数的话,那么则会形成左半边往右倒,右半边往左倒的情况,类似这样,//////////.\\\\\\\\\\

    时间O(n)

    空间O(n) - StringBuilder

    Java实现

     1 class Solution {
     2     public String pushDominoes(String dominoes) {
     3         dominoes = 'L' + dominoes + 'R';
     4         int l = 0;
     5         StringBuilder res = new StringBuilder();
     6         for (int r = 1; r < dominoes.length(); r++) {
     7             if (dominoes.charAt(r) == '.') {
     8                 continue;
     9             }
    10             // 虚拟的牌不放入结果
    11             if (l != 0) {
    12                 res.append(dominoes.charAt(l));
    13             }
    14             int gap = r - l - 1;
    15             if (dominoes.charAt(l) == dominoes.charAt(r)) {
    16                 for (int i = 0; i < gap; i++) {
    17                     res.append(dominoes.charAt(l));
    18                 }
    19             } else if (dominoes.charAt(l) == 'L' && dominoes.charAt(r) == 'R') {
    20                 for (int i = 0; i < gap; i++) {
    21                     res.append('.');
    22                 }
    23             } else {
    24                 for (int i = 0; i < gap / 2; i++) {
    25                     res.append('R');
    26                 }
    27                 if (gap % 2 == 1) {
    28                     res.append('.');
    29                 }
    30                 for (int i = 0; i < gap / 2; i++) {
    31                     res.append('L');
    32                 }
    33             }
    34             l = r;
    35         }
    36         return res.toString();
    37     }
    38 }

    LeetCode 题目总结

  • 相关阅读:
    一:ORM关系对象映射(Object Relational Mapping,简称ORM)
    How to manage concurrency in Django models
    python实现redis三种cas事务操作
    django autocommit的一个坑,读操作的事务占用导致锁表
    Unity3d载入外部图片文件
    MySQL 查询某个列中同样值的数量统计
    Android_自己定义切换控件SwitchView
    SWTBOK測试实践系列(5) -- 项目中使用手动和自己主动化的策略
    自己定义一个Dialog样式的Activity窗体,切换到Dialog的方法
    搜狗语音云开发入门(二)——使用离线语音识别服务
  • 原文地址:https://www.cnblogs.com/cnoodle/p/15945538.html
Copyright © 2020-2023  润新知